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 6675655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:53:04+00:00 2026-05-26T03:53:04+00:00

let a A.java file be : class B {static int i; } class A

  • 0

let a A.java file be :

    class B {static int i; }

    class A {
        public static void main(String[] args) {
            B a=new B();
            B b=new B(); 
            a.i=10;
            b.i=5; 

            System.out.println(a.i);
        }
    }

Why is the result 5 and not 10 ?

Thanks.

  • 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-26T03:53:05+00:00Added an answer on May 26, 2026 at 3:53 am

    Because your variable is static. That means it’s related to the type, not to any particular instance of the type. Your code is equivalent to:

    // These are actually ignored as far as the subsequent lines are concerned.
    // The constructor will still be executed, but nothing is going to read the
    // values of variables "a" and "b".
    B a = new B();
    B b = new B();
    
    // Note this is the *type name*.
    B.i = 10;
    B.i = 5; 
    System.out.println(B.i);
    

    IMO it was a design mistake to allow access to static members via expressions like this – and in some IDEs (e.g. Eclipse) it can end up giving a warning or even an error if you so wish.

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

Sidebar

Related Questions

Let's look at the following Java code. public class Main { public static void
Let's assume I have a file named Main.java with the following code: public class
Let's say I have some Java code: public class SomeClass { static { private
Let's say I have this Hello.scala. object HelloWorld { def main(args: Array[String]) { println(Hello,
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.apache.commons.net.ftp.FTPFile; import java.io.*; public class FTPUpload{ public static boolean
I have a .java src file that looks like this: class Test { public
Let's say I have a structure -bin/com/abc/A.class -src/com/abc/A.java -config/info.txt How to address the file
am trying to run a Java file on Unix machine. Let me show the
I'm not new to Java but I never learned Packages. Anyways, let's say I

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.