• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please explain  RSS feed

 
SwetaSweta Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all who have been to help to me.

Can someone help me understand the below program and what each specified line of code does line by line/step by step? If you can't explain line by line then atleast please explain the meanings of the lines marked in bold. Though it is a simple program but I am finding some difficulty in understanding some of the lines of the code.

Here is the complete program.

class Test{
int a;
Test(int i)
{
a=i;
}
Test incrByTen(){
Test temp=new Test(a+10);
return temp;
}
}
class RetOb{
public static void main(String args[])
{ Test ob1= new Test(2);
Test ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a:"+ ob1.a);
System.out.println("ob2.a:"+ ob2.a);
ob2=ob2.incrByTen();
System.out.println("ob2.a after second increase:" + ob2.a);

}
}
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you get that code from? A pretty awful bit of design, with non-private fields.

What you have is somebody trying to make an immutable class. They haven't succeeded. Make the following changes:
  • class Test becomes "final class Test"
  • int a becomes "private final int a"
  • Add a public int getA() method. The usual sort.
  • Where you write ob1.a or ob2.a, that becomes ob1.getA() or ob2.getA()
  • Now you have an immutable class, which actually is immutable. You cannot change anything in it.

    You cannot add 10 to the value, but what you can do is to add 10 to 2. Your incrByTen method does that; it adds 10 to 2, then it creates a new object with 12 in.

    In an immutable class, you cannot change any of the values. What you can do, however, is create a new object. That is what the incrByTen method does. Every time you use that, you get a new object; if you start from 2 you get an object with 12, then an object with 22, then an object with 32 . . . In this instance you are giving the name ob2 to the new object. Then you are doing the same again.

    Please maintain indentation in your code, and use the code button. you should end up with something like this
     
    Sunil Kumar
    Ranch Hand
    Posts: 81
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    To question:

    creates a new object using the value ob1.a +10 and assigns it to ob2


    Simply prints the value of a in ob2 that would be 12


    creates a new object using the value ob2.a +10 and assigns it to ob2 (ob2.a will be 22)


    prints the value of a in ob2 which would be 22
     
    Campbell Ritchie
    Marshal
    Posts: 56598
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by myself:
    Change that to readAnd please don't call your topics "Please explain".
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!