Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating Objects

 
Deon Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain the advantage or disadvantage of the comment code list for 1 and 2. Thank you

class myClass {
myClass() {
System.out.println("My Class was called");
}


public static void main (String[] args) {
new myClass(); //1
myClass my = new myClass(); //2

}
}
[ September 23, 2002: Message edited by: Deon Smith ]
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. creates an anonymous object. Once the constructor finishes, you can no longer use/access the object or any of it's parts and it becomes immediately available for garbage collection.
2. creates an object and assigns it's reference to a variable. After creation, you can access the object's members, use it's methods, and set it to null when you are done with it.
In your example, since your class has no members or methods, the only difference would be when the object is eligible for garbage collection.
Jamie
[ September 23, 2002: Message edited by: Jamie Robertson ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic