Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

question about static members

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.

Looking at the code below, I thought that it wouldn't compile, because I thought that "y" was an attribute that belongs to Uber class.
But, this code compiles, and when at Minor class I use Minor.y it seems that y belongs to Minor too.

Am I right? y belongs to Minor class now as well as Uber class?


tks
 
Ranch Hand
Posts: 68
MyEclipse IDE Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
y belongs to Uber class...

when you execute this code...compiler will replace the Minor.y with Uber.y
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can see from the code below, if we refer to y with its full qualified name (Uber.y) the code will be more readable.....but like you I'm also confused about the (Minor.y) , can any one elaborate this ??


 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Until a subclass hides it, a static field or method is available through the class itself, its subclasses and instances of all those classes.
So, what is you problem in accessing it?
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumit Khurana wrote:y belongs to Uber class...

when you execute this code...compiler will replace the Minor.y with Uber.y



So is it the cause that we can't override or overload a static method from superclasses?

I'd like to show an example so I can be sure that I've grasped it.
So let me show an example with some comments that I think is right concepts for me now:



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

Abimaran Kugathasan wrote:Until a subclass hides it, a static field or method is available through the class itself, its subclasses and instances of all those classes.
So, what is you problem in accessing it?



Abhimaran can you point to an article that explains this in more detail? Like everyone I was confused with this question. I thought it will not compile because I assumed that y would need to be accessed using Uber.y
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seema Kekre wrote:

Abimaran Kugathasan wrote:Until a subclass hides it, a static field or method is available through the class itself, its subclasses and instances of all those classes.
So, what is you problem in accessing it?



Abhimaran can you point to an article that explains this in more detail? Like everyone I was confused with this question. I thought it will not compile because I assumed that y would need to be accessed using Uber.y




The JLS section 6.3 talks about scoping -- and the "inheriting" of fields into scope is one of the options. This section is followed by hiding rules, which of course, makes the fields out of scope.

Unfortunately, everything else about this part of the JLS is somewhat confusing.
Henry

 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry. I found it in section 8.2.1.1 Example: Inheritance with Default Access in JLS. (Edited my previous reply cause I was too quick to reply. Oops ).
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seema Kekre wrote:
Abhimaran can you......



Is this me? And, cleared your doubts?
 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes doubts cleared
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me explain it as simple as possible:

Now y is declared as:



This means default modifier for y. In other words any class extending Uber will be able to use y as its own member. That is why you can freely use y in Minor in the same way you can use it in Uber. Another thing is you can use Minor.y because y is a static variable. You don't need to even instantiate the Minor class like in line 11. Let me know if anything is not clear.
 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anchit Herredia wrote:
In other words any class extending Uber will be able to use y as its own member. .


This is partially correct. Any class extending Uber in the same package can use its default members as its own, static or non-static.

Edit: Highlighting as its own to be clear
 
Saloon Keeper
Posts: 13504
305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, all classes in a package, and only classes in this package, can access default members of another class in the same package.
 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Actually, all classes in a package, and only classes in this package, can access default members of another class in the same package.


True but not as its own, you will have to make an instance of that class to access the default members unless it is a subclass in the same package. Refer to original post for context. y is used in Minor as if declared locally.
 
Anchit Herredia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seema Kekre wrote:

Anchit Herredia wrote:
In other words any class extending Uber will be able to use y as its own member. .


This is partially correct. Any class extending Uber in the same package can use its default members as its own, static or non-static.

Edit: Highlighting as its own to be clear



Thanks. I love this forum!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic