File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Access modifiers and packages

Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 224
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();
public static void main(String[] args)
{ System.out.println("Hello World!");
} <identifier> expected
^ package obj does not exist

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:


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

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.
I agree. Here's the link:
subject: Access modifiers and packages
It's not a secret anymore!