How to Access Static Member of a Class

How to access static members of a class?

You've declared the static members fine, but not defined them anywhere.

Basically what you've said "there exists some static member", but never set aside some memory for it, you need:

int A::x = 100;

Somewhere outside the class and not inside main.

Accessing static members of a class within static methods

The first thing you need to understand is that static members belong to a class and not an instance and can therefore be accessed directly without the need to create a reference to an instance of the class. The following statement accesses the static member x in class sample where sample is the class name and x is the static member in sample :

sample.x=0;

The following statement on the other hand does not work because
the reference cchild is not static and is thus an instance field while main is a static method. An instance field cannot be accessed in a static method without a reference to an instance of the class.

cchild.x=9

For the above statement to work, you either declare cchild as static in JavaApplication1 or create an instance of JavaApplication1 in main as shown below :

JavaApplication1 instanceOfJApp = new JavaApplication1();
instanceOfJApp.cchild.x=9;

Access a static member of a class through an object

You use the dot:

inst.staticmember

:: is only for use with namespaces or classes, as you may have discerned from your compiler error.

You can access static member variables two ways: either through classy::staticmember where classy is a class, or inst.staticmember where inst is a class instance.

How do I access static member of a class?

If A is a class, you can access it directly via A::$strName.

class A {
public static $strName = 'A is my name';
}

echo A::$strName; // outputs "A is my name"

Update:

Depending on what you have inside your array, whether its what I like to define as class objects or class literals could be a factor. I distinguish these two terms by,

$objClasses = array(new A(), new B()); // class objects
$myClasses = array('A','B'); // class literals

If you go the class literals approach, then using a foreach loop with PHP5.2.8 I am given a syntax error when using the scope resolution operator.

foreach ($myClasses as $class) {
echo $class::$strName;
//syntax error, unexpected '::', expecting ',' or ';'
}

So then I thought about using the class objects approach, but the only way I could actually output the static variable was with an instance of an object and using the self keyword like so,

class A {
public static $strName = 'A is my name';

function getStatic() {
return self::$strName;
}
}

class B {
public static $strName = 'B is my name';

function getStatic() {
return self::$strName;
}
}

And then invoke that method when iterating,

foreach($objClasses as $obj) {
echo $obj->getStatic();
}

Which at that point why declare the variable static at all? It defeats the whole idea of accessing a variable without the need to instantiate an object.

In short, once we have more information as to what you would like to do, we can then go on and provide better answers.

Explicitly access static member variable in static member method - in C++

Is there something like self:: in C++ ?

No there is no such feature, but you can use a class local typedef:

class MyClass {
typedef MyClass self;
static int testValue;
static int getTestValue1(){
return self::testValue;
}
};

See a working demo.

Using Static Member Function to Access Static Data Member

I don't understand why the compiler keeps throwing me this error:

Because you repeated static in the member function definition. The function is already declared static; C++ syntax requires you to omit static in the cpp file:

int Complex::counter = 0;
// Get number of objects existing in the current program
int& Complex::getNumObjects(){
return counter;
}

Note: It is probably not a good idea to return int&, because the callers would be able to modify counter without your knowledge:

Complex::getNumObjects() = -123; // <<== returning reference makes this legal

This is very bad, because it completely breaks encapsulation. Essentially, your counter ends up being exposed as if it were a public member variable.

You should change your function to return int instead:

int Complex::getNumObjects(){
return counter;
}

How can we access static variable without class name

Static variable are always qualified with class name

First of all, it is not true you have to qualify with a class name, you can use a static import for instance:

import static java.lang.Math.PI;

Next you can refer to Math.PI simply by using PI. For example:

import static java.lang.Math.PI;

public class Foo {

public static void main (String[] args) {
System.out.println(PI);
}

}

More information can be found here.

Next as long as you are in the scope of the class, all static members can directly be addressed without having to qualify. In other words this code fragment:

public class Foo {

public static int static_member;

//within this scope you can call static_member without Foo.

}

C++ Access private static member from public static method?

The response to your question is yes ! You just missed to define the static member TheVar :

int MyClass::TheVar = 0;

In a cpp file.

It is to respect the One definition rule.

Example :

// Myclass.h
class MyClass
{
public:
static int DoSomethingWithTheVar()
{
TheVar = 10;
return TheVar;
}
private:
static int TheVar;
};

// Myclass.cpp
#include "Myclass.h"

int MyClass::TheVar = 0;


Related Topics



Leave a reply



Submit