Reputation: 150
I'm new to OOP concepts in Java. What is the difference between these two incidents?
1.
ClassName obj_name = new ClassName();
obj_name.method();
2.
new ClassName().method();
A good explanation is much appreciated. Thanks
Upvotes: 1
Views: 140
Reputation:
Upvotes: 0
Reputation: 419
While they might accomplish the same objective for the first call, the two approaches do fundamentally different things in terms of class definition and instantiation.
ClassName object = new ClassName();`
object.method();`
This is a case of instantiation. You create a new ClassName object which possesses certain instance fields and methods. It can call these methods, and the result may cause its instance fields to change.
ClassName.method();
On the other hand, this approach does not create an instance of the class. Instead, it calls method
as a class attribute. Thus, the result can change fields in ClassName, but it won't necessarily existing fields in already-instantiated objects.
public class ClassName(){
public int attr = 0;
public ClassName(){}
public void setAttr(int value){
this.attr = value;
}
public void method(int value){
this.attr += value;
}
}
Now, using the first approach, we can create a newObj
and call newObj.method(100)
. This will increase newObj
's instance variable attr
by 100.
To see the difference between the two approaches, let's use setAttr(200)
to change the object's attr
to 200.
Next, if we just use ClassName.method(100)
, the class's attr
value will become 100 for all future instances of ClassName. So if we create ClassName nextObj = new ClassName()
, this new instance of ClassName will have attr
of 100, while newObj
will still have attr
of 200.
Hopefully this explains the core difference between the two approaches.
Upvotes: 0
Reputation: 1011
The reference variable obj_name
hold the object of ClassName
.through which you can call the instance method of ClassName through reference variable obj_name
.
Whene ever we create an object and dont assign its reference to any reference variable its Known as Anonymous object instantiation.The Advantage of this type of instantiation is that you can only do the limited operation on that.Like you can call a single method. If you want to perform mopre operation then reference varaibale which hold the object is better approach.If you have multiple method in your class and you want to use them then option 1 is right choice.
for details please go through this Link
Upvotes: 0
Reputation: 22422
In Option(1), you are still having/holding the reference to the object, so you can reuse that reference to access/call the other members(method/variables) of the object (class).
In Option (2), you don't have the reference (i.e., reference has been lost), so you will NOT be able to use it again.
One point to remember is that if you want to access the same object members multiple times, you need to hold the reference (use option 1 above), rather than creating the object (option 2) again and again (which is costly operation i.e., occupies memory).
Please refer the below link for more details: https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html
Upvotes: 6