By type name, you mean the Class
not the Object
. That is a fundamental distinction in Java (and Object Oriented Programming in general).
Objects are instantiated from classes. Class-specific definitions apply to the class methods and static data that are always available for that class in general and don't refer to any one particular instance. Class data and methods can be invoked from anywhere in your app, at anytime, by prefixing with the method or data item with the class name itself. Class-specific methods and data are easily recognized by the static
keyword in front of them in the source code, meaning they never change. They are the same forever in each instance.
You'll get a compile time error if you try to invoke a class-specific method or access class-specific data without the class name prefix.
Whereas Objects are class instances. Objects are created when you use the new
keyword to instantiate a class. That gives you a reference to a new object instance. With that reference, whether you assign it to a variable or not, you can reference all the non-static (non-class level) data and methods.
public class Foo {
public static int myStaticVarPublic = 1;
private static int myStaticVarPrivate = 2;
public int myInstanceVarPublic;
private int myInstanceVarPrivate;
public Foo() {
myInstanceVarPublic = 3;
myInstanceVarPrivate = 4;
}
int getMyInstanceVarPrivate() {
return myInstanceVarPrivate;
}
static int getMyStaticVarPrivate() {
return myStaticVarPrivate;
}
public static void main(String args[]) {
System.out.println("class-specific value Foo.myStaticVarPrivate = " +
Foo.getMyStaticVarPrivate());
System.out.println("class-specific value Foo.myStaticVarPublic = " +
Foo.myStaticVarPublic);
System.out.println("object-specific value myInstanceVarPrivate = " +
new Foo().getMyInstanceVarPrivate());
System.out.println("object-specific value myInstanceVarPublic = " +
new Foo().myInstanceVarPublic);
}
}