• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Java Puzzle for You

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the error say? Or is the point of the exercise to guess what the compiler says?
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kuldeep what u said is wrong as I have instantiated the class Puzzle1 .
So thats not a problem .
 
Sim Kim
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aarthi,

Why no error for 'i' then ?
'i' is also static .
 
Sim Kim
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Kuldeep Vaishnav
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was really Final Word Pradeep .
 
reply
    Bookmark Topic Watch Topic
  • New Topic