• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with super ...

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I wrote simple a program:



when I run i I get:

i 2; super.i: 2

should't I get

i 1; super.i: 2
 
author
Posts: 23951
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

Line 9 is merely two different ways to access the same variable. You don't have two variables -- as there is only one declaration.

Henry
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emilian Chmiel wrote:when I run i I get:

i 2; super.i: 2

should't I get

i 1; super.i: 2

  • No, If you see here
  • The formal parameters int a has value 1 and int b has 2 value passed by this code
  • then in that constructor of second we have
  • Which assigns value of a i.e. 1 to instance variable i of super class first. Now we have i = 1
  • super is the keyword used to access field of super class, here instance variable int i of super class first.


  • But here on the same line
  • We again assigned i = b where value of b is 2. Now value of i is 2.


  • After above assignments, we called to print() method which prints
  • So i and super.i both means same variable of class first which has value 2 so you get output i 2; super.i: 2

  •  
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Name of class always starts with capital letter as you named UseSuper, so becomes easy to identify it. Worth reading Java Programming Style Guide  
     
    The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic