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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:09:49+00:00 2026-06-11T14:09:49+00:00

According to Java, static variable are accessible by Class name but they are also

  • 0

According to Java, static variable are accessible by Class name but they are also accessible by class object even though Java don’t suggest it, and it gives the same answer.

I know there will be only one copy of the variable and its value will be same for all objects and other things. Why does Java suggest to use class name instead of class object?

  • 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-06-11T14:09:50+00:00Added an answer on June 11, 2026 at 2:09 pm

    Because it can be confusing! There is no dynamic dispatching on static members.

    Take a look at this confusing code: (might be syntax errors; my Java is rusty)

    public abstract class Singer {
        public static void sing() {
            System.out.println("Singing");
        }
    }
    
    public class Soprano extends Singer {
        public static void sing() {
            System.out.println("Singing in the range of C4-A5");
        }
    }
    
    public class MyDriver {
    
        public static void main(String[] argv) {
            Singer  mySoprano1 = new Soprano();
            Soprano mySoprano2 = new Soprano();
            mySoprano1.sing();
            mySoprano2.sing();
        }
    
    }
    

    Looking at MyDriver it’s confusing because it seems like the sing method is polymorphic so the output should be…

    Singing in the range of C4-A5
    Singing in the range of C4-A5
    

    … because both soprano1 and soprano2 are instances of Soprano – not Singer.

    But alas, the output is actually:

    Singing
    Singing in the range of C4-A5
    

    Why? Because there is no dynamic dispatch on static members, so the declared type of mySoprano1 determines which sing method is invoked… and the declared type of soprano1 is Singer, not Soprano.

    For more, check out Puzzle 48 “All I get is static” in the book Java Puzzlers.

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

Sidebar

Related Questions

According to the documentation of java.util.Pattern , the POSIX character class \p{Graph} ( [:graph:]
I have a Java class in which every method was defined as static .
According to the documentation : [ java.lang.reflect. ] Proxy provides static methods for creating
I usually don't need to ask Java questions, but I'm stuck more than ever
I started to write my first Java program . class HelloWorldApp { public static
I tested with java.util.IdentityHashMap , See public class IdentityHashMapTest{ public static void main(String args[])
Is it possible in Java to create a static factory method/class that uses an
import static java.lang.System.out; import java.io.FileNotFoundException; import java.io.PrintWriter; public class ShadowingByImporting { public static void
According to http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods%28%29 , Class.getDeclaredMethods() should only include methods declared by the class. However,
In Java, the following code is fine, no error. class ConstantA{ public static String

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.