aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes scope question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "scope question" Watch "scope question" New topic
Author

scope question

Carlos G�mez
Ranch Hand

Joined: Sep 06, 2006
Posts: 56
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

Joined: Nov 22, 2006
Posts: 1
just change the main method,,, ReSort your Application...
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Sep 06, 2006
Posts: 56
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

Joined: Nov 19, 2006
Posts: 133

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 ]

SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCEA/OCMJEA 5.0
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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.


~Sanjeev Singh<br />SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: scope question
 
Similar Threads
Protected Modifier Question
Doubts on Protected Members
doubt
simple general
problem related modifier "protected"