Reputation: 33
Below two points are mentioned in java doc:
My question is: Does class method and class variable belong to "java.lang.class" object in java?
Upvotes: 2
Views: 1872
Reputation: 2223
All classes have methods that are inherited from the Object class.
The code below iterates over all the public variables and methods of SomeClass and outputs them.
You will notice from the output that someVariable and someMethod() belong to our class, while all of the others belong to the Object class.
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test
{
public static void main( String args[] )
{
for ( Field field : SomeClass.class.getFields() )
{
System.out.println( field );
}
for ( Method method : SomeClass.class.getMethods() )
{
System.out.println( method );
}
}
class SomeClass
{
public String someVariable;
public void someMethod()
{
}
}
}
Output:
public java.lang.String com.Test$SomeClass.someVariable
public void com.mailings.classes.Test$SomeClass.someMethod()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
Upvotes: 0
Reputation: 1180
Yes.
This may help....
Every Object in Java belongs to a certain class. That's why the Object class, which is inherited by all other classes, defines the getClass()
method.
getClass()
, or the class-literal - Foo.class
return a Class object, which contains some metadata about the class:
Upvotes: 0
Reputation: 33544
1. Class methods
and Class variable
are the static members of the class, which belongs to the class, they are being shared by all the objects of the class.
2. non-static variables and methods in a class
, belongs to the objects. Every object has their own of these non-static members.
3. java.lang.Class<T>
represent classes
and interfaces
in a running Java application.
4. Class objects contain runtime representations of classes. Every object in the system is an instance of some Class, and for each Class there is one of these descriptor objects. A Class descriptor is not modifiable at runtime.
Upvotes: 1
Reputation: 11598
Yes. Every instance of java.lang.Class permits access to both class variables and methods of that class.
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html
Upvotes: 1