Question for the Java guys

Status
Not open for further replies.
Am I correct in saying:

You can only access an object from the method that created it.

For example, this doesn't compile..
public class Test

{

public static void main(String[] args)
{
Car Porsche = new Car();
Porsche.makeNoise();

anotherMethod();
}

static public void anotherMethod()
{
Porsche.makeNoise();
}


}
Or am I just being stupid :p

Thanks
 
code:<HR>
Code:
public class Test 
{<P> static Car Porsche;<P> public static void main(String[] args)
 {
    Porsche = new Car();
    Porsche.makeNoise();<P>    anotherMethod();
 }<P> static public void anotherMethod()
 {
    Porsche.makeNoise();
 }
}

If Porsche is decalred in a scope where anotherMeod can see it (i.e. a static class variable for a static method, then it will be able to call it wherever it was actually created.
 
Status
Not open for further replies.