Q. Different ways to create objects in Java
ANS : There are following different ways to create objects in java:
1. Using new keyword
This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.
This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.
MyObject object = new MyObject();
2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.
If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
Class.forName().newInstance() is using the reflection API to create an object.
3. Using clone()The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();
4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
5.Using classloader
this.getClass().getClassLoader().loadClass(“com.amar.myobject”).newInstance();
6. Using factory methods
Ex:- NumberFormat obj=NumberFormat.getInstance();
Now you know how to create an object. But its advised to create objects only when it is necessary to do so.
Q. What if I do not provide the String array as the argument to the method?
Ans :Program compiles but throws a runtime error “NoSuchMethodError”.
Q.What if I write static public void instead of public static void?
Ans : Program compiles and runs properly.
Q.What if the static modifier is removed from the signature of the main method?
Ans : Program compiles. But at runtime throws an error “NoSuchMethodError”
Q. What if the main method is declared as private?
Ans : The program compiles properly but at runtime it will give “Main method not public.” message.
Q.Which package is always imported by default?
Ans : The java.lang package is always imported by default.
Q.Is “abc” a primitive value?
Ans :The String literal “abc” is not a primitive value. It is a String object.
Q.What modifiers can be used with a local inner class?
Ans : A local inner class may be final or abstract.
Q.Which class should you use to obtain design information about an object?
Ans : The Class class is used to obtain information about an object’s design.
Q.What is the purpose of the System class?
Ans : The purpose of the System class is to provide access to system resources
System.out.println.......
Q.For which statements does it make sense to use a label?
Ans : The only statements for which it makes sense to use a label are those statements that can enclose a break or continue statement.
Q.Does a class inherit the constructors of its superclass?
Ans : NO A class does not inherit constructors from any of its super classes.
Q.Can a Byte object be cast to a double value?
Ans : No, an object cannot be cast to a primitive value.
Q.What happens to the static fields of a class during serialization? Are these fields serialized as a part of each serialized object ?
Ans : Yes the static fields do get serialized. If the static field is an object then it must have implemented Serializable interface. The static fields are serialized as a part of every object. But the commonness of the static fields across all the instances is maintained even after serialization.
Q.Objects are passed by value or by reference ?
Ans : Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object.
Q. What if I do not provide the String array as the argument to the method?
Ans :Program compiles but throws a runtime error “NoSuchMethodError”.
Q.What if I write static public void instead of public static void?
Ans : Program compiles and runs properly.
Q.What if the static modifier is removed from the signature of the main method?
Ans : Program compiles. But at runtime throws an error “NoSuchMethodError”
Q. What if the main method is declared as private?
Ans : The program compiles properly but at runtime it will give “Main method not public.” message.
Q.Which package is always imported by default?
Ans : The java.lang package is always imported by default.
Q.Is “abc” a primitive value?
Ans :The String literal “abc” is not a primitive value. It is a String object.
Q.What modifiers can be used with a local inner class?
Ans : A local inner class may be final or abstract.
Q.Which class should you use to obtain design information about an object?
Ans : The Class class is used to obtain information about an object’s design.
Q.What is the purpose of the System class?
Ans : The purpose of the System class is to provide access to system resources
System.out.println.......
Q.For which statements does it make sense to use a label?
Ans : The only statements for which it makes sense to use a label are those statements that can enclose a break or continue statement.
Q.Does a class inherit the constructors of its superclass?
Ans : NO A class does not inherit constructors from any of its super classes.
Q.Can a Byte object be cast to a double value?
Ans : No, an object cannot be cast to a primitive value.
No comments:
Post a Comment