Reputation: 93
I am really not sure whats happening in this code. Basically, in the current state, the code outputs 0
, but I am expecting the output to be 20
. I only get 20
when I use the other part of the OR which is currently commented out:
class Sum {
int x, y;
void setSum(int value1, int value2) {
x = value1;
y = value2;
}
int getSum() {
return x + y;
}
}
public class JavaBasics {
public static void main(String[] args) {
// Sum newSum = new Sum();
// newSum.setsum(10, 10); // 10 , 10 are the arguments
//
// OR
new Sum().setsum(10, 10);
System.out.println(new Sum().getSum());
// OR
// System.out.println(newSum.getSum());
}
}
Upvotes: 2
Views: 193
Reputation: 14746
class Sum {
int x, y;
void setSum(int value1, int value2) {
x = value1;
y = value2;
}
int getSum() {
return x + y;
}
}
When you define any class nothing will happen in memory, not a single allocation. Memory allocation will be based on instances you defined for that class like,
Sum s1 = new Sum();
Sum s2 = new Sum();
Here two memory blocks will be created, in each block there are two variables say x and y. Remember instance variables bounded with object and methods are commonly shared by all the objects.
So when you called s1.getSum()
then it will gives you x and y of s1 object because you have called this method using s1 (invoking object is accessible inside the method).
So if you want to store value in variables then you need to create object for it, new Sum()
is having single line scope after execution get completed it will not accessible.
Sum s1=new Sum(); // Create an instance
s1.Setsum(10, 10); // Invoking set value method using s1 object so 10, 10 will be stored in x and y of s1.
System.out.println(s1.getSum()); // display x and y of s1 object.
Upvotes: 0
Reputation: 767
new Sum().Setsum(10, 10); // Object 1
System.out.println(new Sum().getSum()); //Object 2
In first Object creation, you are passing 10
and 10
to setSum()
method. Those values are assigned to x
and y
respectively inside setSum()
method. In your code declared x
an y
as a instance variables.
In second object, you are calling gerSum()
method . returning x+y
. They already initialized with 10 and 10 by using Object1. That's why its returning 20
.
Solution:
Sum s1 = new Sum();
s1.setSum(10,10);
s1.getSum(); // using same object s1
Output:
20
Upvotes: 0
Reputation: 1548
The problem is the Object is not hold by any reference.
new Sum().Setsum(10, 10);
When you used this syntax then a object(memory location) is created and along with a set method is called. but we do not have any reference to point to the memory.
system.out.println(new Sum().getSum());
When you used this syntax then a new object(memory location) is created and along with get method is called. this object does not have any relationship with first one.
new Sum().Setsum(10, 10).getSum();
You can use this syntax if you do not want to keep reference.
I hope it helps you to understand the concept.
Upvotes: 0
Reputation: 127
You are not getting the result as 20 because . When u r calling using anonymous object ..that object having no reference so that u can not point to that object .. Means whenever u r using only new sum().setsum() ..that time it is setting the value . But when u are trying to the get u r calling by new sum().getsum(). Which is new object..it is not pointing to the same instance what u used for setting the sum.. So that's why u r getting 0. So better to use reference . Means sum newSum= new sum(). And do the setting and getting by using that reference variable newSum.get Sum(); Thank u
Upvotes: -1
Reputation: 35557
You are referring two different object to set values and get values back.
new Sum().Setsum(10, 10); // object 1
System.out.println(new Sum().getSum()); // object 2
you are setting value in first step. But you are referring some other new instance to get values back, There is no values set to this new instance.
Make sure you are referring to same object then you will get expected result.
Sum sum=new Sum(); // creating a instance
sum.Setsum(10, 10); // setting value
System.out.println(sum.getSum()); // getting value back from same instance
Upvotes: 3
Reputation: 41281
You are making two different instances of Sum
, which have their own state, each time you use new Sum()
.
Since you don't have a constructor the implicit default one is used, that sets x and y to both be zero when the object is constructed.
By storing it to the variable Sum newSum
, you are creating a Sum
, setting its x and y, and then reading back that same Sum
.
You can declare some or all fields static to make their value be accessible or shared across all instances. You can likewise make a static
method that can be called without an instance of the class (e.g. ClassName.staticMethod(foo, bar)
). Of course, since there is no instance it can't directly access non-static fields.
Upvotes: 9
Reputation: 466
You print out
new Sum().getSum()
This creates a new Instance of your Class Sum, which of course has the default values 0 and 0.
I guess you want something like:
Sum mySum = new Sum().setSum(10,10)
System.out.println(mySum)
Upvotes: 1
Reputation: 52
Of course it would be 0. You use different instances of Sum class. Change your main method to something like this:
Sum sunObj = new Sum(); sumObj.Setsum(10, 10);
System.out.println(sumObj.getSum());
Upvotes: -1