Sizhnor Rhena

Greenhorn
+ Follow
since Nov 17, 2003
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sizhnor Rhena

Here is the error message I got when I compile
Outer.java:5: non-static variable this cannot be referenced from a static context
Inner i = new Inner();
^
1 error
in order to make this code work we need to add static in the inner class then only static void main method can access inner class. static only acces static!
here is working code!
public class Outer {
public String name = "Outer";
public static void main(String argv[])
{
Inner i = new Inner();
i.showName();
}//End of main
private static class Inner {
String name =new String("Inner");
void showName()
{
System.out.println(name);
}
}//End of Inner class
}
Thanks Vad & Kathy !
Now It is clear to me.
In the code below drive()method in parent and child both are static so what I undersatan from previous posting is static method can't be overridden , redeclared or redefined . So what is drive() method in child class in the code below ?
class Car {
public static void drive(){ //1
System.out.println("I drive Car in parent class");
}
}
class Hammer extends Car {

public static void drive(){ // 2
System.out.println("I drive Hammer in child class!");
}
static void main(String[] args){
Hammer H2 = new Hammer();
//call drive method from parent class Car
Car.drive();//upcasting
//call drive method from child class Hammer
H2.drive();
}
}
[ November 17, 2003: Message edited by: Sizhnor Rhena ]
[ November 17, 2003: Message edited by: Sizhnor Rhena ]
I am getting confused with overridden Vs. redeclared/ redefined ?
K& B book Say:
"Static methods cannot be overridden, although they can be redeclared/ redefined by a subclass. So although static methods can sometimes appear to be overridden, polymorphism will not apply "
so in this code below //2 is overridden or redeclared/ redefined ?
class Car {
public static void drive(){ //1
System.out.println("I drive Car");
}
}
class Hammer extends Car {

//The variables in java can have the same
//name as method or class
static String Hammer = "Arnold";
public static void drive(){ // 2
System.out.println("I drive Hammer!");
System.out.println(Hammer);
}
static void main(String[] args){
Hammer H2 = new Hammer();
//call drive method from parent class Car
Car.drive();//upcasting
//call drive method from child class Hammer
H2.drive();
}
}