• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on Static Method  RSS feed

 
Santosh kameswara
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was my first post in Code Ranch ..

1 . Y static method can be called with called with classname .. though it can be called with object .. i mean background with static methods

2 . Class Test
{
Public ..... main()
{
Test t = null ;
t.fun();
}
public static fun()
{ }
}

In this case it shuld throw null pointer exception in case of normal method but still it works fine .. How can this be possible .. ?



Thanks in Advance ..


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh Jk wrote:This was my first post in Code Ranch ..

1 . Y


Please UseRealWords.(⇐click)


In this case it shuld throw null pointer exception in case of normal method but still it works fine .. How can this be possible .. ?


Because the JLS says it is. Basically, since Java only cares about the class, and doesn't need an object, the compiler looks at the type of reference t, sees its declared of type Test, and turns the call into Test.fun().
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search this forum (and “Java™ in General”) because there have been several threads about static methods. About two or three weeks ago.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!