Computing.Net > Forums > Programming > Private java

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Private java

Reply to Message Icon

Name: Peter
Date: November 7, 2003 at 12:14:54 Pacific
OS: win 98
CPU/Ram: 128
Comment:

Hi!
I have some problem with JAVA.I read many java books but I still have problem
with something like(private ,public protected..).
Can you tell me how Privat, public.. are working, please help me.
Tanks
Peter



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: November 7, 2003 at 15:50:48 Pacific
Reply:

public variables can be accessed directly from another class:

class Test
{
  public int x;
}

class Main
{
  Test mytest = new Test();
  mytest.x=10;//this is legal
}

private variables can not.
class Test
{
  private int x;
}

class Main
{
  Test mytest = new Test();
  mytest.x=10;//this is an error
}

Hope this helps,
-SN


0

Response Number 2
Name: deity_me
Date: November 7, 2003 at 20:55:07 Pacific
Reply:

public class Parent{
public int myPublic;
protected int myProtected;
private int myPrivate; //=)
//stuff like constructors and methods
}
public class NotChild{
Parent myParent = new Parent();
//this will always work
int parentPublic = myParent.myPublick;
//this wont work
int parentProtected = myParent.myProtected;
//this will never work
int parentPrivate = myParent.myPrivate;
}
public class YesChild extends Parent{
//this will always work
int parentPublic = myParent.myPublick;
//this work because it can see protected variables
int parentProtected = myParent.myProtected;
//this will never work
int parentPrivate = myParent.myPrivate;
}

public - anyone can see and/or modify
protected - only child classes can see
private - nothing outside the class can use that variable or method

hope that helps


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


array input problem in c+... Domino Designer



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Private java

Sine wave generation using Java www.computing.net/answers/programming/sine-wave-generation-using-java/12012.html

JAVA collision detection etc www.computing.net/answers/programming/java-collision-detection-etc/14141.html

please help me whit this java code www.computing.net/answers/programming/please-help-me-whit-this-java-code/11640.html