• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how data hiding

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A
{
int i,j,k;
H(int i)
{
i++;
}
H(int i,int j)
{
i++;
j++;
}
H(int i,int j,int k)
{
i++;
j++;
k++;
}
}
class Manager5
{
public static void main(String args [])
{
H h1 = new H(1);
H h2 = new H(1,2);
H h3 = new H(1,2,3);
System.out.println(h1.i);
System.out.println(h2.i+ ":" + h2.j);
System.out.println(h3.i+ ":" + h3.j+ ":" +h3.k);
}
the output of following is 0
0:0
0:0:0


can anybody please explain why constructor inside is not incrementing ?
whatever be the reason please explain elaborately.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can anybody please explain why constructor inside is not incrementing ?



Because it won't compile. But if you fix that, then the constructor WILL increment the variables, just not the variables you think it should.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

It's incrementing the local variables. If you want to refer to the member variables when you have locals with the same name, you have to qualify the variable names with "this."
 
bikasit babu
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

can anybody please explain why constructor inside is not incrementing ?



Because it won't compile. But if you fix that, then the constructor WILL increment the variables, just not the variables you think it should.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

It's incrementing the local variables. If you want to refer to the member variables when you have locals with the same name, you have to qualify the variable names with "this."



hello first off all thanks for replying but the mention code compiled fine and output also whatever i have mentioned
my question is why having the output as i was mention.can you give me some brief explanation
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

bikasit babu wrote:the mention code compiled fine



No, it does not. I promise you.

my question is why having the output as i was mention.can you give me some brief explanation



I gave you an explanation. What part didn't you understand?
 
bikasit babu
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

bikasit babu wrote:the mention code compiled fine



No, it does not. I promise you.

my question is why having the output as i was mention.can you give me some brief explanation



I gave you an explanation. What part didn't you understand?


OK Thank you ,for your reply.I got the part which is wrong.
Its by mistake .instead of H h1 = new H(1); replace H and Put A or existing class name in all Object creation and run
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

bikasit babu wrote:OK Thank you ,for your reply.I got the part which is wrong.
Its by mistake .instead of H h1 = new H(1); replace H and Put A or existing class name in all Object creation and run



Yes. And now we're up to the second part of my earlier reply, where I answered your question. So, once again, what part of that explanation do you not understand?
 
bikasit babu
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

bikasit babu wrote:OK Thank you ,for your reply.I got the part which is wrong.
Its by mistake .instead of H h1 = new H(1); replace H and Put A or existing class name in all Object creation and run



Yes. And now we're up to the second part of my earlier reply, where I answered your question. So, once again, what part of that explanation do you not understand?


Actually i didn't get your explanation. can you come again with the explanation in more easy way.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

bikasit babu wrote:

Jeff Verdegan wrote:

bikasit babu wrote:OK Thank you ,for your reply.I got the part which is wrong.
Its by mistake .instead of H h1 = new H(1); replace H and Put A or existing class name in all Object creation and run



Yes. And now we're up to the second part of my earlier reply, where I answered your question. So, once again, what part of that explanation do you not understand?


Actually i didn't get your explanation. can you come again with the explanation in more easy way.



I don't know how to make it any easier. What in particular did you not understand?

Did you click the link I provided that explains the different kinds of variables?

Do you understand the difference between local variables and member variables in Java?
 
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff is correct when he says that it is incremented, you can check that by using a print statement in the constructor like this will show you that i has changed. But it is not the same as h1.i.


 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

bikasit babu wrote:


original source, with code tags. MUCH easier to read.
 
All of the following truths are shameless lies. But what about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic