File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scope question

 
Carlos G�mez
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ranchers.
someone can i help me ?
I do not understand why the next code compiles ?

if you delete the static modifier of the line1 does not compile, what is the rule ?


package packed;
public class pack
{
static public int x1 = 7;
static protected int x2 = 8; // line1
static int x3=9;
static private int x4 =10;
}


package finalscjptiger;
import packed.pack;

class test extends pack
{
public static void main( String args[] )
{
pack p = new pack();
System.out.println( pack.x2 );
}
}

Thank in advance
 
man mann
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just change the main method,,, ReSort your Application...
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is that the main method is always in a static context. If you remove the static modifier from the variable, it becomes an instance variable and can't be accessed in a static context.
 
Carlos G�mez
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wrote:

package packed;
public class pack
{
static public int x1 = 7;
static protected int x2 = 8; // line1
static int x3=9;
static private int x4 =10;
}


package finalscjptiger;
import packed.pack;

class test extends pack
{
public static void main( String args[] )
{
pack p = new pack();
System.out.println( pack.x2 ); // line 2
}
}

Im sorry ranchers, i found a mistake in the question ... you must change the next line:
System.out.println( p.x2 ); // line 2

If you change the line, i do not understand why the code compiles ?
if you delete the static modifier on the line1 does not compile, what is the reason ?
 
Krzysztof Koziol
Ranch Hand
Posts: 133
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because x2 is protected so if it's not static it could be only accessed through sublcass test.

try this:

pack p = new test();
System.out.println( p.x2 );
[ November 22, 2006: Message edited by: Krzysztof Koziol ]
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you delete the static modifier of the line1 does not compile, what is the rule ?

Rule 1: The protected member can be accessible to the classes which are declared in same package in which the class declaring the member exists or accessible to the sublclss of this class.
Rule 2: protected members can be accessed by their subclassed only through inheritence.
if the member is instance variable or instance methods they has to follow both the rules while for the members which can not be inherited like (static members and constructors) has to follow the rule 1 only.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic