aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static and non-static can be in the same class...they will fight:-)) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static and non-static can be in the same class...they will fight:-))" Watch "static and non-static can be in the same class...they will fight:-))" New topic
Author

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

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
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:


Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
Jason Cox
Ranch Hand

Joined: Jan 21, 2004
Posts: 287
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

Joined: Aug 26, 2002
Posts: 205
Hi,
You posted a nice question.


Narasimha
Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90

“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.
 
subject: static and non-static can be in the same class...they will fight:-))