• Post Reply Bookmark Topic Watch Topic
  • New Topic

java question  RSS feed

 
john mor
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I want to learn java but I have a few questions.
1- I know about data type(Int, double, float....),
but I do not know how an object works like a
type, and what does it mean? How does it work?

class Object{
object myObject;
}
--------------------------------------
2- Object reference
Box b1=new Box;
Box b2=b1;

I know b1 and b2 both have the same value because they
refer to the sme Object(box). but if we have this:
Box b1=new Box;
Box b2=b1;
b1=null;
//b1 has been set to null, but b2
//still points to the original object.
my question is if b1=null, but why b2 still points to orignal object(Box)
---------------------------------------------
3- I have problem whit private keyword. I know what
dose it it mean , but I do not know how dose it
work and when we should write private. and how can we hide our code whit private.
thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
1) An Object is like a container. It has methods (functionality) and fields (data). I'm using a different, but similar example to yours, since Object is defined by Java and special.


I defined a Person class. A Person has a name. It also has a reference to another person, its mother.

2) In line one, you create an object and point to it. Let's pretend it's an apple on the table. In line two, you create another pointer. Now we have two people pointing at the apple on the table. In line three, one of the people stops pointing to it. So there is one person (b2) pointing to the apple on the table. You wouldn't expect the apple to cease to exist just because one person stopped pointing at it. If you set b2 to null, there will be nobody pointing at it. At that point, the garbage collector is free to throw out the object since nobody cares about it anymore.

3) Private means that no one outside your class can access the field or method. In my example for #1, people have to call your getXXX and setXXX methods instead of directly accessing the fields. This hides the implementation type. I can later decide that I want to store first name and last name separately. The callers in other classes wouldn't need to know I changed anything because it is private (aka non-public) data.
 
john mor
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne Boyarsky!
Thank you very much.
John
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!