Results 1 to 9 of 9
  1. #1
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Why its print child static variable ?

    Java Code:
    class A
    {
    
       static int y=4;
    
    }
    
    class B extends A
    {
       static int y;
       {
    	   y=5;
       }
       
    
    
    }
    
    
    public class Exr
    {
        public static void main(String Dev[])
    	{
    	    B b=new B();
    		System.out.println("eeee"+b.y);
    	
    	}
    
    }

    i just want to know that why it is print child class static variable....

    is this any rule for this static variables....

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Why its print child static variable ?

    It prints the 'y' variable in class B because that is the class reference 'b' is.
    If you did:
    Java Code:
    A b=new B();
    then it would print the value of the static variable in A.

    Which is why you should reference static things using the class name and not an instance variable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why its print child static variable ?

    And remember that when subclassing a class, variables are not overridden but hidden. So see my related
    post #5 here -> https://www.java-forums.org/new-java...plzz-help.html

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: Why its print child static variable ?

    You should get a filed hides another field warning for this. It's not going to break the code, but it is considered a bad practice to have the same variable, with the same name in parent and child class. It confuses people you work with, and it will confuse you also. You already have a variable y in your class A, and you inherit that field in your child class B. If you want to declare more variables in class B get a different, exclusive names.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Why its print child static variable ?

    It's a static variable. It isn't inherited.
    But yes, you really don't want to hide variables you know about (ie would otherwise be visible), and you really shouldn't access static things using an instance reference.

    Will the compiler warn about this, or is that simply an IDE thing?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: Why its print child static variable ?

    Agreed, inappropriate use of word inherited. Should have said, visible.
    Yes, it's IDE thing. I think it passes compiling and executing.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why its print child static variable ?

    I think you're right. I checked javac and -Xlint does not catch the hiding.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: Why its print child static variable ?

    Yes, just tried. Runs smoothly. IDE just gives you it's warning.

    Why its print child static variable ?-hiding.jpg

  9. #9
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: Why its print child static variable ?

    im doing practise and just create senearios if this happen then what to do ...i also puzzled when i crearte....sooo

Similar Threads

  1. Replies: 2
    Last Post: 04-10-2013, 12:46 AM
  2. Replies: 7
    Last Post: 12-07-2012, 01:00 PM
  3. Replies: 5
    Last Post: 12-01-2010, 06:43 AM
  4. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •