We have printed integer, float, double, String, Boolean type variables till now using System.out.println(" "); statements. But have your tried printing an object of any Class type. If not lets do this below -
Example -
ClassOne object1 = new ClassOne( ); //Object 'object1' is created
System.out.println(object1); //'object1' object is being printed.
Lets implement printing an object using Eclipse IDE -
1. Launch Eclipse IDE, Create a Java Project 'Project 34' as shown below -
2. Create a new Java Class file 'ClassXYZ' with main( ) method as shown below -
3. Create a new Class 'ClassOne' in the same 'ClassXYZ' Java Class file as shown below -
4. Create an integer instance variable 'var1' inside the 'ClassOne' class as shown below -
5. Create an object for 'ClassOne' class in 'ClassXYZ' class as shown below -
6. Print the object as shown below -
7. Save & Run the Java Class file 'ClassXYZ' and view the output in the console as shown below -
Observe that the Class name of the printed object followed by some hash code is displayed in the console as output as shown above.
Printing the value of the instance variable in 'ClassOne' on printing its object will be explained in the upcoming posts.
In the next post, lets use toString( ) method of Object Class to print an object.
Please comment below to feedback or ask questions.
No comments:
Post a Comment