Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static and non-static can be in the same class...they will fight:-))  RSS feed

 
Harvinder Singh
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wednesday, February 11, 2004
I think, When the static method test is called then the non-static print is not visible.Then how can the error duplicate method can come.
public class Test {
public static void test() {
print();
}
public static void print() {
System.out.println("Test");
}
public void print() {
System.out.println("Another Test");
}
}

Few tips about the intiallization will be really very helpful I think just opposite to the compiler.... :roll:
 
Jason Cox
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaring a method static does not overload or override the method.
There is nothing there that the compiler will recognize that makes it a different method.
 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You posted a nice question.
 
Harvinder Singh
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

“There is nothing there that the compiler will recognize that makes it a different method. “
[\quote]
There is one difference that static method can be called with a class level variable or a instance level. And instance method could be called with a instance level variable only.Why compiler doesn’t uses this fact to select the correct method.
I can understand that when the code is changed to:
[code]
public class Test {
public static void test() {
(new Test()).print();
}
public static void print() {
System.out.println("Test");
}
public void print() {
System.out.println("Another Test");
}
}
[\code]
Then both the print () method are visible since both the static and non static could be called with an instance of the class.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!