This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access modifiers and packages

 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cannot reference obj outside a method.

Change your code to the following:



Jeremy
 
Srilakshmi Vara
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic