Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6802537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:14:07+00:00 2026-05-26T19:14:07+00:00

Let’s look at the following code snippet in Java. package trickyjava; class A {

  • 0

Let’s look at the following code snippet in Java.

package trickyjava;

class A
{
    public A(String s)
    {
        System.out.println(s);
    }
}

final class B extends A
{
    public B()
    {
        super(method());      // Calling the following method first.      
    }

    private static String method()
    {
        return "method invoked";
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        B b = new B();
    }
}

By convention, the super() constructor in Java must be the first statement in the relevant constructor body. In the above code, we are calling the static method in the super() constructor parameter list itself super(method());.


It means that in the call to super in the constructor B(), a method is being
called BEFORE the call to super is made! This should be forbidden by the compiler but it works nice. This is somewhat equivalent to the following statements.

String s = method();
super(s);

However, it’s illegal causing a compile-time error indicating that “call to super must be first statement in constructor”. Why? and why it’s equivalent super(method()); is valid and the compiler doesn’t complain any more?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:14:08+00:00Added an answer on May 26, 2026 at 7:14 pm

    The key thing here is the static modifier. Static methods are tied to the class, instance methods (normal methods) are tied to an object (class instance). The constructor initializes an object from a class, therefore the class must already have been fully loaded. It is therefore no problem to call a static method as part of the constructor.

    The sequence of events to load a class and create an object is like this:

    1. load class
    2. initialize static variables
    3. create object
    4. initialize object <– with constructor
    5. object is now ready for use

    (simplified*)

    By the time the object constructor is called, the static methods and variables are available.

    Think of the class and its static members as a blueprint for the objects of that class. You can only create the objects when the blueprint is already there.

    The constructor is also called the initializer. If you throw an exception from a constructor and print the stack trace, you’ll notice it’s called <init> in the stack frame. Instance methods can only be called after an object has been constructed. It is not possible to use an instance method as the parameter for the super(...) call in your constructor.

    If you create multiple objects of the same class, steps 1 and 2 happen only once.

    (*static initializers and instance initializers left out for clarity)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a class like this: public class Person { private String
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say you have the following class: class A { private Foo foo =
Let us assume I have two classes: class Base{}; class Derived: public Base{}; none
Let's say I have the following code: Sub TestRangeLoop() Dim rng As Range Set
Let's say I have the following within my source code, and I want to
Let's say I just wrote the following class and I am starting to write
Let's say I have the following type type Key = String type Score =
Let's consider the following expressions in Java. byte a = 32; byte b =
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.