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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:16:32+00:00 2026-05-26T15:16:32+00:00

By convention, a static method specifically in Java can have access only to static

  • 0

By convention, a static method specifically in Java can have access only to static fields or other static methods. The following simple code snippet however appears to violate the convention. Let’s consider the following simple code snippet in Java.

class Super
{
    protected static int x;
    protected static int y;

    public Super(int x, int y)
    {
        Super.x=x;
        Super.y=y;
    }

    public static int sum()
    {
        return(x+y);
    }
}

final class Sub extends Super
{
    public static int temp=100;
    public Sub(int x, int y)
    {
        super(x, y);
    }

    public void concreateMethod()
    {
        System.out.println("\nInstance variable x = "+x);
        System.out.println("Instance variable y = "+y);
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        Sub s=new Sub(10, 5);
        System.out.println("\nAssociating with object x = "+s.x);
        System.out.println("Associating with object   y = "+s.y);

        System.out.println("\nAssociating with class name x = "+Sub.x);
        System.out.println("Associating with class name   y = "+Sub.y);

        System.out.println("\nSummation (Associating with object)   = "+s.sum());
        System.out.println("Summation (Associating with class name) = "+Sub.sum());

        System.out.println("\nAssociating with class name temp = "+Sub.temp);
        System.out.println("Associating with object temp =    = "+s.temp);

        System.out.println("\nConcreate method called.");
        s.concreateMethod();
    }
}

The above code produces the following output with the respective statements.

Associating with object x = 10
Associating with object y = 5


Associating with class name x = 10
Associating with class name y = 5


Summation (Associating with object) = 15
Summation (Associating with class name) = 15


Associating with class name temp = 100
Associating with object temp = = 100


Concreate method called.
Instance variable x = 10
Instance variable y = 5


The static fields s and x are being accessed through the following statements within the main() method using the object of the Sub class, though they are declared as static in the super class Super.

Sub s=new Sub(10, 5); 
System.out.println("\nAssociating with object x = "+s.x);
System.out.println("Associating with object   y = "+s.y);

The following statements of course, have no doubt.

System.out.println("\nAssociating with class name x = "+Sub.x);
System.out.println("Associating with class name   y = "+Sub.y);

Since x and y are static, they can certainly be accessed in this way.


The same is the method call, observe the following statements.

Sub s=new Sub(10, 5);
System.out.println("\nSummation (Associating with object)   = "+s.sum());
System.out.println("Summation (Associating with class name) = "+Sub.sum());

Both of the ways, the static method sum() is being accessed using the object of the class Super and also using the class name Sub.


Again the similar case with the static field temp declared within the Sub class

System.out.println("\nAssociating with class name temp = "+Sub.temp);
System.out.println("Associating with object temp =    = "+s.temp);

The static field temp is being accessed in both the ways.


Why is this happening here?

  • 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-26T15:16:32+00:00Added an answer on May 26, 2026 at 3:16 pm

    Basically it’s a flaw in the design of Java IMO which allows static members (methods and fields) to be referenced as if they were instance members. This can be very confusing in code like this:

    Thread newThread = new Thread(runnable);
    newThread.start();
    newThread.sleep(1000);
    

    That looks like it’s sending the new thread to sleep, but it actually compiles down into code like this:

    Thread newThread = new Thread(runnable);
    newThread.start();
    Thread.sleep(1000);
    

    because sleep is a static method which only ever makes the current thread sleep.

    Indeed, the variable isn’t even checked for non-nullity (any more; it used to be, I believe):

    Thread t = null;
    t.sleep(1000);
    

    Some IDEs can be configured to issue a warning or error for code like this – you shouldn’t do it, as it hurts readability. (This is one of the flaws which was corrected by C#…)

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

Sidebar

Related Questions

Is there a Java convention to refer to methods, static and otherwise, any specific
You can have different naming convention for class members, static objects, global objects, and
A protected class member in Java by convention, can be accessed only from within
In Java, static final variables are constants and the convention is that they should
Okay, so i have this code i wrote: class Connection { public static StreamWriter
Let's look at the following code snippet in Java. package trickyjava; class A {
I have a naming strategy for denoting the nature of code entity (variable, method,
We have a static method in a utility class that will download a file
EDIT: As of Java 8, static methods are now allowed in interfaces. Here's the
Is there a convention for naming the private method that I have called _Add

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.