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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:30:58+00:00 2026-06-05T19:30:58+00:00

public class Knowing { static final long tooth = 343L; static long doIT(long tooth)

  • 0
public class Knowing {

    static final long tooth = 343L;

    static long doIT(long tooth) {
        System.out.print(++tooth + " ");
        return ++tooth;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.print(tooth + " ");
        final long tooth = 340L;

        new Knowing().doIT(tooth);
        System.out.println(tooth);
    }
}

Okay so here is my question:

  • If we have a global variable declared static final long tooth = 343L; How can we have another variable in the main method declared final long tooth = 340L; I just want to know why this is allowed because I ran it and there was no error?

  • And also shouldn’t the access of the global static variable tooth be by using className.variableName and not by creating a new instance.variable name how come that is allowed with only a warning?

  • 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-05T19:31:01+00:00Added an answer on June 5, 2026 at 7:31 pm

    How can we have another variable in the main method declared final long tooth = 340L;

    Because the language specification says you can. From section 6.4.1 of the JLS, about shadowing:

    A declaration d of a local variable or exception parameter named n shadows, throughout the scope of d, (a) the declarations of any other fields named n that are in scope at the point where d occurs, and (b) the declarations of any other variables named n that are in scope at the point where d occurs but are not declared in the innermost class in which d is declared.

    Should you do this? Rarely. On the other hand, I’ve rarely seen it be a problem, either.

    For your second question:

    And also shouldn’t the access of the global static variable tooth be by using className.variableName and not by creating a new instance.variable name how come that is allowed with only a warning?

    This is a design flaw in Java, IMO. Even the warning isn’t part of the language specification. You should always avoid doing this, as it makes the code do something other than it looks like it’s doing. The example I usually give is:

    Thread backgroundThread = new Thread(someRunnable);
    backgroundThread.start();
    backgroundThread.sleep(1000);
    

    That makes the executing thread sleep, not the new thread.

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

Sidebar

Related Questions

public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
public class User { public long Id {get;set;} [References(typeof(City))] public long CityId {get;set;} [????]
Let's say I have a factory ressembling this: public static class BusinessObjectFactory { public
public class tryAnimActivity extends Activity { /** * The thread to process splash screen
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class Test1<Type> { public Type getCompositeMessage(Type... strings) { Type val = (Type) ;
public class WeeklyInspection : Activity { WebView view = (WebView) FindViewById(Resource.Id.inspectionWV); view.Settings.JavaScriptEnabled = true;
public class LinkedList { Object contents; LinkedList next = null; public boolean equals(Object item)
public class HttpPostTask extends AsyncTask<Void, Integer, Void> { TextView txtStatus = (TextView)findViewById(R.id.txtStatus); @Override protected

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.