aspose file tools*
The moose likes Java in General and the fly likes Access modifiers and packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Access modifiers and packages" Watch "Access modifiers and packages" New topic
Author

Access modifiers and packages

Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 215
I am new to java. I am learnning access modifers and acess control. Thisis my error


package cert;
public class Base
{
int i;
public int j;
protected int k;
private int l;
}
package stuff;
import cert.*;
public class Child extends Base
{
Base obj=new Base();
obj.j=11;
public static void main(String[] args)
{ System.out.println("Hello World!");
}
}
Child.java:7: <identifier> expected
obj.j=11;
^ Child.java:7: package obj does not exist
obj.j=11;

Since, j is public varable in base class. So it is legal to access
in child class even though it is another package
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
you cannot reference obj outside a method.

Change your code to the following:



Jeremy


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Srilakshmi Vara
Ranch Hand

Joined: Jul 21, 2004
Posts: 169
obj.j=11;

Keep the above statement in any method in the constructor or method, it works.

As Jeremy said you cannot reference non static variables outside a method or constructor.
 
 
subject: Access modifiers and packages