1
class SomeClass
{
    static public void someStaticMethod(){};
}

SomeClass SomeClass=new SomeClass();
SomeClass.someStaticMethod();

How to call the static method of any class where object name is same as class name?

6
  • Does this even pass compilation? Commented Jan 28, 2013 at 15:52
  • 1
    Why would you have the class name and object name same on the first place? Commented Jan 28, 2013 at 15:52
  • 2
    prepend the package name to the class. i.e com.mypackage.SomeClass.someStaticMethod(); Commented Jan 28, 2013 at 15:53
  • Also: the object name should explain why this object exists. If it's just a random someClass then it shouldn't exist anyway. Is it the "target" of something, the "source", the "frobnicator"? Having variable names that are equal to class names (even if they differ in case) is a slight code smell in my opinion. Commented Jan 28, 2013 at 15:55
  • @Isaac yes, that is what made me curious Commented Jan 28, 2013 at 15:55

3 Answers 3

4

You don't have to do anything - it will work as is (although it makes little sense to use such a confusing code).

How ambiguous names are handled is specified in the JLS #6.5.2. In particular:

If the AmbiguousName is a simple name, consisting of a single Identifier:

  • If the Identifier appears within the scope (§6.3) of a local variable declaration (§14.4) or parameter declaration (§8.4.1, §8.8.1, §14.20) or field declaration (§8.3) with that name, then the AmbiguousName is reclassified as an ExpressionName.
  • [...]
  • Otherwise, if a type of that name is declared in the compilation unit (§7.3) containing the Identifier, either by a single-type-import declaration (§7.5.1), or by a type-import-on-demand declaration (§7.5.2), or by a single-static-import declaration (§7.5.3), or by a static-import-on-demand declaration (§7.5.4), then the AmbiguousName is reclassified as a TypeName.

So in your case, SomeClass will be the variable you declared one line above, which has priority over types.

Sign up to request clarification or add additional context in comments.

Comments

3

The variable name should not be capitalized, hence you would have:

SomeClass.someStaticMethod();

for the static one and:

someClass.someNonStaticMethod();

for the non-static versions.

Comments

0

Do not start instance names with capital letter !

You should write :

SomeClass someClass=new SomeClass();

or better yet :

SomeClass someObject=new SomeClass();

If you follow these very common rules, you won't have this issue. (I'm trying to guess where this '-1' comes from !!!)

2 Comments

yes, i know. but i am curious if there is any other way around or not as stating both class and object same does not show any compilation error.
Well you should try by youself and tell us the answers. However, this is only possible if you keep this very bad coding habits... (the -1 just disappeared ;) never mind)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.