Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Conceptual Questions Related To "super" keyword and "constructor".

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Conceptual Questions Related To "super" keyword and "constructor".

    Hi there,

    I hope everybody is in good state of health. I have a couple of questions and want to know the reasoning behind them. Sincie I am a beginner to Java, and have studied till Inheritance, I have some questions related to it, given below;

    1. What is the reason the we can use static code and data in non-static method but we can't use non-static code and data in static methods?

    2. Why can't we use "super" keyword in Main method?

    3. In using "super" with constructor, why must the calling the constructor of Super Class must be the first statement in the child class' one?

    I hope that the experts on JPF would help me solve these conceptual questions. I know that Java won't allow me to do what I have stated above. But I want to know the reasoning behind it.

    Thanks for your time and I hope that you'ld reply on your earliest.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Conceptual Questions Related To "super" keyword and "constructor".

    Static means that all members of a class share the same container for a value.
    For example: a class named Family has a variable: public static int cookieJar;
    When mary (an instance of Family) or bobby (another instance of Family) access cookieJar, they access the same cookieJar, thus changing the number of cookies in the jar for the whole family.

    1a. Little mary, being a member of Family, always has access to the cookieJar, and she should, it is the cookieJar shared by the Family.
    1b. A static method just does not know which object's values to change unless a reference is passed in.
    public static final Cookie getCookie(handInJar) {
       if(handInJar == mary.getCookieHand) {
          if(time > bedtime) {
             return noCookie;
          }
          return smallCookie;
       }
       return bigCookie;
    }
    How will cookieJar know anything about mary? Where is this information stored? The Family class would have to maintain a reference to every member. What would this method do before little mary was even born?

    2. What would super refer to from the main method?

    3. The super class must be fully constructed first. When you do not include super(); or a call with parameters, then super(); is automatically called as the first line of the constructor anyway. If you wish to call, for example super(bundleSavedState); then you must do so before super(); is automatically called. This is the reason we the users must call it on the first line of the constructor.
    The reason it is the first thing done is because the new class can not be an object of the super class until the super constructor returns. Only then can the new class begin to exist. If super was initialized later, the new class would have no access to anything from super() during it's own construction. What if super() happens to set values needed by the new class? The only way would be to let super construct first.

  3. The Following 2 Users Say Thank You to jps For This Useful Post:

    angstrem (May 26th, 2013), wikki2013 (May 26th, 2013)

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  2. [SOLVED] Adding the "final" keyword makes difference, I'm confused with the sequence flow.
    By SmokyBrain in forum Object Oriented Programming
    Replies: 2
    Last Post: April 30th, 2012, 01:50 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM