Java Tutorial/Class Definition/Class Object
Содержание
- 1 Assignment with objects is a bit tricky.
- 2 Class Object
- 3 Demonstrate Run-Time Type Information.
- 4 Getting java.lang.Class: Information about your object
- 5 Storing a reference to the String object as type Object
- 6 The "Class" class also brings the possibility of creating an object without using the new keyword.
Assignment with objects is a bit tricky.
class Number {
int i;
}
public class MainClass {
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
n1.i = 9;
n2.i = 47;
System.out.println("1: n1.i: " + n1.i + ", n2.i: " + n2.i);
n1 = n2;
System.out.println("2: n1.i: " + n1.i + ", n2.i: " + n2.i);
n1.i = 27;
System.out.println("3: n1.i: " + n1.i + ", n2.i: " + n2.i);
}
}
1: n1.i: 9, n2.i: 47 2: n1.i: 47, n2.i: 47 3: n1.i: 27, n2.i: 27
Class Object
- All the classes defined are subclasses by default.
- Object is a superclass of every class.
- The inheritance happens automatically.
- Your classes will inherit members from the class Object.
MethodPurposetoString()returns a string describing the current objectequals()compares two objectsgetClass()returns a Class that identifies the class of the current object.hashCode()calculates a hashcode for an objectnotify()wakes up a thread associated with the current object.notifyAll()wakes up all threads associated with the current object.wait()causes a thread to wait
class Dog{
public Dog(String aType){
}
}
public class MainClass{
public static void main(String[] a){
Dog d = new Dog("a");
Class objectType = d.getClass();
System.out.println(objectType.getName());
}
}
Dog
Demonstrate Run-Time Type Information.
class X {
int a;
float b;
}
class Y extends X {
double c;
}
class MainClass {
public static void main(String args[]) {
X x = new X();
Y y = new Y();
Class<?> clObj;
clObj = x.getClass();
System.out.println("x is object of type: " + clObj.getName());
clObj = y.getClass();
System.out.println("y is object of type: " + clObj.getName());
clObj = clObj.getSuperclass();
System.out.println("y"s superclass is " + clObj.getName());
}
}
Getting java.lang.Class: Information about your object
public class MainClass {
public static void main(String[] a) {
String country = "Canada";
Class myClass = country.getClass();
System.out.println(myClass.getName());
}
}
java.lang.String
Storing a reference to the String object as type Object
A variable of type Object can store a reference to an object of any class type.
public class MainClass {
public static void main(String[] a) {
String saying = "A stitch in time saves nine.";
Object str = saying;
System.out.println(str);
System.out.println(str.getClass().getName());
}
}
A stitch in time saves nine. java.lang.String
The "Class" class also brings the possibility of creating an object without using the new keyword.
- The static forName method creates a Class object of the given class name.
- The newInstance method creates a new instance of a class.
public class MainClass {
public static void main(String[] a) {
Class klass = null;
try {
klass = Class.forName("java.lang.String");
} catch (ClassNotFoundException e) {
}
if (klass != null) {
try {
// create an instance of the Test class
String test = (String) klass.newInstance();
} catch (IllegalAccessException e) {
} catch (InstantiationException e) {
}
}
}
}