This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Resilient Oracle PL/SQL: Building Resilient Database Solutions for Continuous Operation and have Stephen Morris on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Please explain

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

}
}
 
Marshal
Posts: 77903
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    Ranch Hand
    Posts: 81
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 77903
    373
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by myself:

    Change that to readAnd please don't call your topics "Please explain".
     
    "Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic