aspose file tools*
The moose likes Beginning Java and the fly likes how data hiding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how data hiding" Watch "how data hiding" New topic
Author

how data hiding

bikasit babu
Greenhorn

Joined: Mar 29, 2012
Posts: 29
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Greenhorn

Joined: Mar 29, 2012
Posts: 29
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Greenhorn

Joined: Mar 29, 2012
Posts: 29
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Greenhorn

Joined: Mar 29, 2012
Posts: 29
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
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.


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

bikasit babu wrote:

original source, with code tags. MUCH easier to read.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how data hiding