aspose file tools*
The moose likes Beginning Java and the fly likes can static method be overloaded ? reason? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can static method be overloaded ? reason?" Watch "can static method be overloaded ? reason?" New topic
Author

can static method be overloaded ? reason?

vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
hi all,

I am new to this forum.. i have a question

question :
can static method be overloaded ? reason?

please answer this anyone........

Thanks,
Vinay
Shiva Battula
Greenhorn

Joined: Jun 06, 2005
Posts: 20
Yes. Its simple question if you dont mind


SCJP and SCWCD.
Suman Sharma
Ranch Hand

Joined: May 16, 2005
Posts: 72
Yes, static methods can be overloaded. The following example illustrates this.

class Test
{
static String Greet(String name)
{
String msg = "Good Morning! " + name;
return msg;
}
static int Greet(int nm)
{
return (nm*nm);
}
public static void main(String[] args)
{
Test ts = new Test();
System.out.println(ts.Greet("Abhishek"));
System.out.println("Square value: " +ts.Greet(12));
}
}
class Test2 extends Test
{
static String Greet(String nm)
{

String msg = "Good Evening! " + nm;
return msg;
}
public static void main(String[] args)
{
Test2 ts2 = new Test2();
Test ts1 = new Test();
System.out.println(ts2.Greet("Abhishek"));
System.out.println(ts1.Greet("Sharad"));
System.out.println("Square value :" + ts1.Greet(2));
}
}

In the class Test, the static Greet methods are overloaded.
Suman Sharma
Ranch Hand

Joined: May 16, 2005
Posts: 72
I am sorry. Please consider the following code:
The static methods are accessed by qualifying it with their class name, not object name.

public class Test {

static String Greet(String name)
{
String msg = "Good Morning! " + name;
return msg;
}
static int Greet(int nm)
{
return (nm*nm);
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//Test ts = new Test();
System.out.println(Test.Greet("Abhishek"));
System.out.println("Square value: " + Test.Greet(12));

}

}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

"Shiv", you have previously been warned on one or more occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Be aware that accounts with invalid display names are removed.

bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
prasad valeti
Greenhorn

Joined: Oct 18, 2005
Posts: 7
overriding concept is for only object behaviours not class level behaviours.The following example is working fine but that is not come under
overriding concept because static is class level not object level.

class A{
static void test(){
System.out.println("in class A");
}
}
class B extends A{
static void test(){
System.out.println("in class B");
}}
class C{
public static void main(String s[]){
A a = new B();
a.test(); // in class A
A a = new A();
a.test();// in class A
B b = new B();
b.test(); // in class B
}
}
if static methods are overridden at that moment it gives "in class B " but in the above example it is giving "in class A" means here considering

the referencing A not the object(new B()).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can static method be overloaded ? reason?