aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "static methods" Watch "static methods" New topic
Author

static methods

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
just thought i would post this in case someone finds it interesting or informative. both versions compile as written.

VERSION 1 //cant use this or super in body of static method
class Test1 extends Test{
static String string1 = "string1";
public static void main(String[] args) {
Test1 test1 = new Test1();
Test.method1();
((Test)test1).method1();
//super.method1(); //undefined variable, class or package name: super
test1.method1();
//this.method1(); //undefined variable, class or package name: this
}
static void method1() {
System.out. println("Test1");
}
}
class Test {
static void method1() {
System.out.println("Test");
}
}

VERSION 2 //variable test1 is out of scope
class Test1 extends Test{
static String string1 = "string1";
public static void main(String[] args) {
Test1 test1 = new Test1();
test1.method2();
}
static void method1() {
System.out. println("Test1");
}
void method2() {
Test.method1();
//((Test)test1).method1(); //undefined variable, class or package name: test1
super.method1();
//test1.method1(); //undefined variable, class or package name: test1
this.method1();
}
}
class Test {
static void method1() {
System.out.println("Test");
}
}



SCJP
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: static methods
 
Similar Threads
Constant specific class body
Instance Method/Static Method/Field Hiding
Static variables
overriding ??
in Overriding