• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Static methods and Exceptions

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I ran this code I expected a Null Pointer Exception, instead
it printed out A ???

class An
{
static public void prt() { System.out.println("A"); }
}

public class Test
{
static An an = null;
public static void main(String[] args)
{
an.prt();
}
}
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are class methods, so they don't care if the reference variable points to null.

In this case:

an.prt();

is equivalent to:

An.prt();
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So Many previous Thread having a same discussion, In case for Static method or variable compiler will change the object Instance with this Class name.

In your case
an.prt(); will replace by An.prt(); at compile time only. So At Run time JVM will not encounter any problem like null or other..
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have to read more about the static keyword and its uses. Static is class level.
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple thing to remember while working with Static Methods.
Static methods/variables belong to class and not to a particular object.

Take your case for example

An an = null;
an.prt();

In this case the object an is replaced by its reference.
SO the function call becomes An.prt(). And the question of the object pointing to null doesnt come into picture, since we are never using the object.

A simple call to prt() would have also sufficed.

Read more about "Overidding vs Shadowing in Static methods". This will help you in grasping the subtlities about the static keyword.
 
straws are for suckers. tiny ads are for attractive people.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic