Reputation: 89
I recently encountered this phrase:
"Class A has class member int a"
Probably obvious but this sentence just means a
is an int
defined in class A
, right?
And another thing, for example a
is defined under a method in class A
. is it still
a class member?
I haven't found a clear definition of class member, I looked here:
but it wasn't very helpful.
Thank's in advance for the help
Upvotes: 1
Views: 6326
Reputation: 1
Variable in Java is a data container(memory) that stores the data values during Java program execution.There are 3 types of variables in java. They are local variables,instance variables,static variables. local variables - declared within the body of a method.. instance variables - declared inside the class but not inside the method,to access these variables you need to create an object static - memory allocated only once..directly accessible and its not object specific Static variables defined at global scope of the class and so they also refereed as class member.for example
public class TypesofVar {
int a = 10; // instance variables
static int c = 30; // static variables
public static void main(String[] args) {
int b = 20; // local variable
System.out.println(c);
System.out.println(b);
TypesofVar obj = new TypesofVar();
System.out.println(obj.a);
}
}
What you have asked is that is int a inside methodA() still a class member? NO because it is not preceded by static keyword
Upvotes: 0
Reputation: 11225
In the docs link you have mentiond, its clear in the first line (after heading) that
In this section, we discuss the use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class.
So it means that static
keyword is used to create class fields and methods(i.e.class members).
So in your case,
class A{
int a;
public void methodA(){
int a;//inner a
}
}
What you have asked is that is int a
inside methodA() still a class member?
Answer is no: since it is not preceded by static keyword.If you try to use static keyword as:
class A{
int a;
public void methodA(){
static int a;//inner a will cause compile time error
}
}
You will get compile time error. Hope that helped!! :)
Upvotes: 0
Reputation: 15594
class member is not just a variable of the class. they can be accessed using the class name. That means they are static variable of that class.
The document mentioned it clearly.
public class Bicycle {
private int cadence;
private int gear;
private int speed;
// add an instance variable for the object ID
private int id;
// add a class variable for the
// number of Bicycle objects instantiated
private static int numberOfBicycles = 0;
...
}
in the above code numberOfBicycles is a class member. It can be accessed using
Bicycle.numberOfBicycles
And variables inside methods can't access like that. so they can't be class members. variables declared inside a method are local variables and belong to that method. So you can call them final, but not static or public or protected or private.
Upvotes: 1
Reputation: 122026
Fields that have the static modifier in their declaration are called static fields or class variables
Class variables are referenced by the class name itself, as in
Bicycle.numberOfBicycles
This makes it clear that they are class variables.
Upvotes: 1
Reputation: 328893
Class member is another way of calling static members.
class A {
int a; //instance variable
static int b; //class variable
public void c() {
int d; //local variable
}
}
Upvotes: 8