java - Is there a way to get the object which called my static method as an instance method? -


this question has answer here:

we know in java can call static method instance method this:

foo foo = new foo(); foobar foobar = foo.bar(); // bar static method on class foo 

what want know is:

  • is there way determine inside bar whether bar called statically (foo.bar()) or called via class instance above?
  • if so, there way bar reference object called (in case foo)?

reason:

i developing kind of semantic syntax. want consumers able put things like:

with.attribute("blah").and().attribute("blahblah"); // both "attribute" , "and" methods return object of type "with" 

here can see attribute being called both static , instance method. however, can't define static , instance method same name in java, same reason above - static method called instance method , create instance method same name create ambiguity. therefore want create single method attribute can called both statically , non-statically, , inside method body want try determine if invoked statically or non-statically. above questions me assess feasibility of doing this.

no, there no way method know whether called on class or on instance (at jvm level there no difference), , there no way instance method called on.

the term kind of "semantic syntax" domain-specific language (dsl).

possible solution: name static method withattribute, make this:

withattribute("blah").and().attribute("blahblah"); 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -