Java Reference
In-Depth Information
class Echo {
int count = 0;
}
Now let us create an object of our class Echo and call it in another class.
public class my_class {
public static void main(String[] args) {
Echo e1 = new Echo();
Echo e2 = new Echo();
Int x = 4;
e1,count = x;
e2,count = x;
system.out.println (e1.count + e2.count);
}
}
Explanation :
We have created 2 objects e1 and e2 from class Echo. There is an instance variable count
in this class. The value of this variable will be computed when we create and call objects
from another class my_class. We created a variable x and assigned a value 4 to this vari-
able. Then we assigned value of x to variable count of objects e1 and e2. In the print to user
screen we asked to print value of addition of e1.count and e2.count. The result should be 8
shown on user screen as both e1.count and e2.count hold values of 4 each.
Now let us change some assignments.
public class my_class {
public static void main(String[] args) {
Echo e1 = new Echo();
Echo e2 = new Echo();
Int x = 4;
e1,count = x;
e2 = e1;
e2,count = e2.count + 1;
Search WWH ::




Custom Search