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

Java Puzzle for You

Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268


In Class Puzzle2 : Why there is a compile time error while accessing non-static member 'j' of class Puzzle1 .
[ August 11, 2005: Message edited by: Lavjeet Khanuja ]
Kuldeep Vaishnav
Ranch Hand

Joined: May 23, 2004
Posts: 72
may be because non static member j can not be used in static method main and asign a value in a static manner.May be setJ(int value) would help..aah..thats the best guess i have...

[ August 11, 2005: Message edited by: Kuldeep Vaishnav ]
[ August 11, 2005: Message edited by: Kuldeep Vaishnav ]

Kuldeep
Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
Hi,

In the above program since the variable j is defined as protected it is giving compile time error. This is because protected variables and methods can be accessed by classes and subclasses in same package.
If both the classes are in same package then it wont give compile time error or we have to make variable j as public in puzzle1 class of forReview package. Hope this is correct. If wrong please correct me.

Thanks
ARathi
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What does the error say? Or is the point of the exercise to guess what the compiler says?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mark Wuest
Ranch Hand

Joined: Jun 07, 2003
Posts: 88
Originally posted by Lavjeet Khanuja:
In Class Puzzle2 : Why there is a compile time error while accessing non-static member 'i' of class Puzzle1 .

Are you saying you get this error? This seems a non-sequitor to me, the only error you'd get is accessing the protected j, as others have noted. i is static - look at your code.

HAH! - I took a break and tried it. Changing j to public makes it compile and run.

Mark
Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268
Kuldeep what u said is wrong as I have instantiated the class Puzzle1 .
So thats not a problem .
Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268
Aarthi,

Why no error for 'i' then ?
'i' is also static .
Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268
Stan,

The exercise is why I am getting compile time error ?

Compile time error is : The variable 'j' cannot be accessed .
[ August 11, 2005: Message edited by: Lavjeet Khanuja ]
Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
Originally posted by Lavjeet Khanuja:
Aarthi,

Why no error for 'i' then ?
'i' is also static .


No error for i because protected variable can be accessed by the subclass through an object reference that is the same type as the class or one of its subclasses. Correct me if wrong.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Arathi Raj:


No error for i because protected variable can be accessed by the subclass through an object reference that is the same type as the class or one of its subclasses. Correct me if wrong.


But this doesn't explain why accessing j gives a compiler error. I suspect that it is a combination of the protected and static modifiers and how they interact with static and non-static methods of the subclass.

As an exploration, maybe you can add a non-static method to Puzzle2 and do the same thing. Be sure to call this non-static method from main (after creating an appropriate object, of course). I'm interested to see if it makes a difference.

Sorry that I don't have an answer or any other suggestions.

Layne


Java API Documentation
The Java Tutorial
Kuldeep Vaishnav
Ranch Hand

Joined: May 23, 2004
Posts: 72
Aarthi,
this is what java tutorial says :

http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

quote (from the above said page)
------------------------------------------------------------------------
Access Levels Specifier Class Package Subclass World
private Y N N N
no specifier Y Y N N
protected Y Y Y N
public Y Y Y Y

The first column indicates whether the class itself has access to the memeber defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. A package groups related classes and interfaces and provides access protection and namespace management. You'll learn more about packages in the section Creating and Using Packages. The third column indicates whether subclasses of the class � regardless of which package they are in � have access to the member. The fourth column indicates whether all classes have access to the member.
----------------------------------------------------------------------------
****************************************************************************
----------------------------------------------------------------------------
****************************************************************************
----------------------------------------------------------------------------
Lavjeet,

Please create an instance of Puzzle2 (from the same page) and access the protected variable..
it should work.... hopefully
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
If you go through - Programmer's Guide to Java� Certification by Khalid A. Mughal - you will fid the answer to this problem.

It clearly states - "A subclass in another package can only access protected members in the superclass via references of its own type or its subtypes."

That should be the final word.

Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268
That was really Final Word Pradeep .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java Puzzle for You