• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can static method be overloaded ? reason?

 
vianyrajnish rajnish
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Its simple question if you dont mind
 
Suman Sharma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64716
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
prasad valeti
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic