• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are static variables inherited through constructors?

 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Analyzing the following code snippet.



I don't understand how Minor class can "see" Uber class since static members are not inherited.

Could anyone give me some light? Thanks in advance...
 
Tim Cooke
Sheriff
Pie
Posts: 3203
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using the code tags, please be sure to indent your code properly. It makes it so much easier to read. I've done it for your this time and see how much better it looks?
 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I am so sorry...
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember these rules:static member are shared by every instance of class

And
Instance of subclass IS-A instance of super class

I think these 2 rules will help you solving your doubts
 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not still clear. If add comments to line new Minor();



but the code compiles fine and prints "2".
In this case there is no instance at all. So... is "y" inherited??
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfonso Saenz wrote:I don't understand how Minor class can "see" Uber class since static members are not inherited.

Could anyone give me some light?

Which statement made you conclude that static members are not inherited? Static members in the superclass that are visible to the subclass are inherited (as illustrated in your code snippet). But (and that could be the reason for you confusion): static (class) methods are never overridden. Never! Repeat after me: static (class) methods are never overridden Only instance methods can be overridden (if the instance method is inherited). So that means, a private instance method can never be overridden as well.
A subclass can define a static method with the same signature as a static method in the superclass. That's called hiding: the method in the subclass hides the one in the superclass. Did you know the rules for method overriding apply to these hiding static methods as well, although they can't be overridden? Read this post to find out more.

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfonso Saenz wrote:but the code compiles fine and prints "2".
In this case there is no instance at all. So... is "y" inherited??

As you know from this topic, to access class (static) members you don't need an instance, you just need to have a class. So creating a Minor instance by invoking its no-arg constructor will only have an effect on the value of class (static) field y (as the multiplications and addition won't be executed anymore). To access a class (static) member you only need a class, you don't need an instance at all. So that's why after removing the call to the no-arg Minor constructor, the code still compiles.

What's the result of this code snippet?

Hope it helps!
Kind regards,
Roel
 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because private variables are not inherited, "y" field is not supposed to be seen by Minor. I guess we have compiler error in line "System.out.print(y)".

So... can I conclude that static variables are inherited?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes static variables are inherited like their fellow instance variable s
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfonso Saenz wrote:Because private variables are not inherited, "y" field is not supposed to be seen by Minor. I guess we have compiler error in line "System.out.print(y)".

Almost correct! It doesn't matter if it's an instance variable or a class (static) variable; if it's defined as private, you can only access it in the class in which the variable was defined. Let's apply this rule to the code snippet: the class (static) variable y is private, so this variable can only be accessed in class Uber and all occurences of y in class Minor will result in a compiler error. So not only the line System.out.print(y); but also the lines super(y); and y += 3; (in the no-arg Minor constructor) will not compile.

Alfonso Saenz wrote:So... can I conclude that static variables are inherited?

Like I mentioned in my previous post: static members (both methods and fields) in the superclass that are visible to the subclass are inherited by the subclass. So if a class (static) member is inherited by the subclass depends on both the accessibility of the class (static) member itself and the superclass. Just for the record: this rule applies to instance members as well. For example in this code snippet the class (static) member count will not be inherited by the subclass Y and it's your task to explain why it isn't.

Let's have another pop quiz as well to see if you completely understand everything explained in this topic. What's the result of this code snippet?

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:Yes static variables are inherited like their fellow instance variable s

In the code snippet I don't see any instance variable s being defined. And what you say, is incorrect as well! Instance members (both methods and fields) in the superclass that are visible to the subclass are inherited by the subclass. So if an instance member is inherited by the subclass depends on both the accessibility of the instance member itself and the superclass.
 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first example: I think count is not accessible from subclass because even though class is public, count variable has package private access. So... taking into account that both classes are in different packages, "count" cannot be seen from subclass.

Second example: "y" is now instance variable, then "System.out.print(y)" will give us error compiling, cause I cannot access to "y" variable without real object (instance).

Thank you RdN.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfonso Saenz wrote:For the first example: I think count is not accessible from subclass because even though class is public, count variable has package private access. So... taking into account that both classes are in different packages, "count" cannot be seen from subclass.

Second example: "y" is now instance variable, then "System.out.print(y)" will give us error compiling, cause I cannot access to "y" variable without real object (instance).

Both explanations are spot-on. Well done!
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also y cannot be refered while invoking the parent class constructor , so this line super(y) will also throw a compiler error. and you must have a main method in the public Uber class.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:also y cannot be refered while invoking the parent class constructor , so this line super(y) will also throw a compiler error.

That's only true if y is an instance variable. If y is a class (static) variable, the code will compile successfully (no compiler errors).

Ramya Subraamanian wrote:and you must have a main method in the public Uber class.

Why? There's a valid main method in the Minor class to run the application.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's only true if y is an instance variable. If y is a class (static) variable, the code will compile successfully (no compiler errors).


thats true.

Why? There's a valid main method in the Minor class to run the application.


yeah , but only Uber is public right..

then you must do a

javac Uber
java Minor

commands to access that Minor's main method , because Javac Uber command will create Uber.class and Minor.class

its not a big thing ...but just suggested it

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:yeah , but only Uber is public right..

If you have a close look at all the aforementioned code snippets, you'll notice that both Uber and Minor classes have default (package-private) access.

Ramya Subraamanian wrote:javac Uber
java Minor

commands to access that Minor's main method , because Javac Uber command will create Uber.class and Minor.class

The javac command is incorrect, because it's missing the .java extension for the source code file (should be javac Uber.java). Secondly, because the Uber class is not public, you can't know which source code file name you have to use to compile both classes. Both classes could be defined in a source code file named Source.java and then the appropriate command to compile these classes would be javac Source.java. But you are definitely correct about the result of this javac command Two class files (Uber.class and Minor.class) will be created and in order to successfully execute the application you need to invoke the command java Minor.

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and I want to check if my understanding is correct about instance variables.

Instance variables are created when the constructor is called. if you make a call to Super it calls the parent constructor before the child constructor is called, hence the instance variables are not yet created and hence the compiler throws an error. and if that's so why is it not throwing a compiler error in line 9 as well . .(sorry if this sounds silly)
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:Instance variables are created when the constructor is called. if you make a call to Super it calls the parent constructor before the child constructor is called, hence the instance variables are not yet created and hence the compiler throws an error.

That's not 100% accurate! At the moment an object is created (when the new operator is called), all instance variables exist and are initialized with their default values. So a constructor doesn't create instance variables at all, a constructor can only be used to do some initialization of instance variables. But you can't use an instance variable before the superclass constructor has finished its execution (that's why line8 gives a compiler error if y is an instance variable). If you want to know more about the creation and initialization of instance variables, I highly recommend reading this topic. It provides detailed explanations with a bunch of illustrative code snippets.

Ramya Subraamanian wrote:and if that's so why is it not throwing a compiler error in line 9 as well.

On line9 the superclass constructor has completed its execution, so it's completely valid to access the instance variable at this line.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic