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

  • SEARCH
  • Home
  • 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 7699187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:25:11+00:00 2026-05-31T22:25:11+00:00

I have a class, Super : public class Super { public static String foo

  • 0

I have a class, Super:

public class Super {
    public static String foo = "foo";
}

I also have another class, Sub that extends Super:

public class Sub extends Super {
    static {
        foo = "bar";
    }

    public static void main (String[] args) {
        System.out.println(Super.foo);
    }
}

When I run it, it prints out bar.
My third (and last) class is Testing:

public class Testing {
    public static void main (String[] args) {
        System.out.println(Super.foo);
        System.out.println(Sub.foo);
        System.out.println(Super.foo);
    }
}

This prints:

foo
foo
foo

I don’t understand why the contents of foo vary depending on what class you’re accessing it from. Can anyone explain?

  • 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-31T22:25:12+00:00Added an answer on May 31, 2026 at 10:25 pm

    I don’t understand why the contents of foo vary depending on what class you’re accessing it from.

    Basically it’s a matter of type initialization. The value of foo is set to "bar" when Sub is initialized. However, in your Testing class, the reference to Sub.foo is actually compiled into a reference to Super.foo, so it doesn’t end up initializing Sub, so foo never becomes "bar".

    If you change your Testing code to:

    public class Testing {
        public static void main (String[] args) {
            Sub.main(args);
            System.out.println(Super.foo);
            System.out.println(Sub.foo);
            System.out.println(Super.foo);
        }
    }
    

    Then it would print out “bar” four times, because the first statement would force Sub to be initialized, which would change the value of foo. It’s not a matter of where it’s accessed from at all.

    Note that this isn’t just about class loading – it’s about class initialization. Classes can be loaded without being initialized. For example:

    public class Testing {
        public static void main (String[] args) {
            System.out.println(Super.foo);
            System.out.println(Sub.class);
            System.out.println(Super.foo);
        }
    }
    

    That still prints “foo” twice, showing that Sub isn’t initialized – but it’s definitely loaded, and the program will fail if you delete the Sub.class file before running it, for example.

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

Sidebar

Related Questions

The following code does not compile: public class GenericsTest { public static void main(String[]
class Patmatch { static String strLine=; public static void main(String [] args) { try
import java.io.*; class MyException1 { static String str=; public static void main(String args[]) {
I have the next code: class Foo { public Foo (String param) { ...
I have a super class: public class SuperClass { public void dosomething() { firstMethod();
I have the following code: class SuperClass { public static String getName() { return
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
Given the following code: public static class Super { public static class Inner {
Given this snippet of code public abstract class Foo { private static SqlConnection _sqlConnection;
I have this class used in the API demos: public class TextToSpeechActivity extends Activity

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.