participate


New To Java - Doubt regarding static methods
This question is not answered.

<<   Back to Forum  |   Give us Feedback
This topic has 5 replies on 1 page.
Paul_Erric
Posts:11
Registered: 2/13/09
Doubt regarding static methods   
Nov 4, 2009 1:01 AM
 
 
import java.util.*;
class MyChk{
	public  static String getStr(){
		return ""+new Random().nextInt();
	}
}
public class MyTest04 {
	public static void main(String[] args) {
		try{
			//chkObj object is null
			MyChk chkObj=null;
			System.out.println("\t"+chkObj);
			System.out.println("\t"+chkObj.getStr());
		}
		catch (Exception exception){
			System.out.println("Exception ###\t"+exception.toString());
		}
	}
}

here chk is initalized with null ,why the method call chkObj.getStr() is working
what is the difference between method invocation chkObj.getStr() as shown above and method invocation like MyChk .getStr()
Plz help
 
JoachimSauer
Posts:8,238
Registered: 1/8/04
Re: Doubt regarding static methods   
Nov 4, 2009 1:06 AM (reply 1 of 5)  (In reply to original post )
Helpful
 
Paul_Erric wrote:
here chk is initalized with null ,why the method call chkObj.getStr() is working

Because calling a static method doesn't check the value of the variable at all, but only checks its type.

what is the difference between method invocation chkObj.getStr() as shown above and method invocation like MyChk .getStr()

There is no difference in how it behaves!

That's a good reason to avoid calls like "chk.getStr()". It looks as if the value of chk actually matters for the call, while in fact it doesn't matter at all. You should always call static methods using the class name: "MyChk.getStr()", then it's clear what the intention and behavior is.
 
punter
Posts:116
Registered: 9/8/09
Re: Doubt regarding static methods   
Nov 4, 2009 1:27 AM (reply 2 of 5)  (In reply to original post )
Helpful
 
 
Paul_Erric
Posts:11
Registered: 2/13/09
Re: Doubt regarding static methods   
Nov 4, 2009 9:05 PM (reply 3 of 5)  (In reply to original post )
 
 
Thanks for your fast response
Is an invocation like chkObj.getStr()(chkObj is null and method getSt() is static ) has anything to do with run time polymorphism if so Kindly Explain
 
ejp
Posts:26,309
Registered: 11/5/97
Re: Doubt regarding static methods   
Nov 4, 2009 9:29 PM (reply 4 of 5)  (In reply to #3 )
 
 
If the method is static there is no runtime polymorphism.
 
jverd
Posts:51,846
Registered: 3/30/99
Re: Doubt regarding static methods   
Nov 4, 2009 9:29 PM (reply 5 of 5)  (In reply to #3 )
 
 
Paul_Erric wrote:
Thanks for your fast response
Is an invocation like chkObj.getStr()(chkObj is null and method getSt() is static ) has anything to do with run time polymorphism if so Kindly Explain

No.

It's specifically because static methods are NOT runtime polymorphic that x.foo() works even when x is null. For runtime polymorphic methods--which in Java are non-private, non-final, non-static methods--x has to be non-null. It has to point to an object so the runtime knows which class' implementation of that method to call.
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums