Conceptos básicos de Java: cláusulas de objeto y if

I have a very basic problem with an object.

I create the object in an if() statement in my main method. (there is an else, so it will always be created.)

The Object has a print() method, which works fine, but when I put the myobject.print() outside the if() clauses where it was created I get an error: "cannot find symbol".

I guess I m making a dumb beginner mistake, but the myobject.print() inside the if() clauses will work fine, so my question is, what s happening to my object ? ( The main method does not do anything else in between..)

  else default to 20, 10, and fill randomly 1/4
  {
    int a = 20;
    int b = 10;
    Table myTable = new Table(a,b);
    myTable.randomfill(Math.round((a*b)/4)); //round in case defaults change later
    System.out.println("Printing Table .... ");
    myTable.print();  <-- here it works
  }

  //always print !!
  System.out.println("Printing Table .... ");
  myTable.print(); <-- won't work

preguntado el 10 de marzo de 12 a las 14:03

4 Respuestas

What the others said. If you have the following:

if (x == y) {
    MyObject obj = new MyObject();
}

Entonces obj will not be defined once you leave the {} de los if.

To make the object accessible "outside" you'd do:

MyObject obj = null;
if (x == y) {
    obj = new MyObject();
}

(Note that you DO NOT "declare" obj dentro de {} -- you leave off the leading MyObject.)

respondido 10 mar '12, 14:03

myTable is scoped to else block only. It is not visible after else block completion. If you want to access it outside of else block, you may define myTable outside else block and assign object to it inside else block.

Table myTable =null;
else default to 20, 10, and fill randomly 1/4 
  { 
    int a = 20; 
    int b = 10; 
    myTable = new Table(a,b); 
    myTable.randomfill(Math.round((a*b)/4)); //round in case defaults change later 
    System.out.println("Printing Table .... "); 
    myTable.print();  <-- here it works 
  } 

  //always print !! 
  System.out.println("Printing Table .... "); 
if(myTable != null){  //Make sure myTable is not null.
  myTable.print(); <-- won't work 
}

respondido 10 mar '12, 14:03

A local variable like myTable has scope (is only visible) within the block the code where you declare it. The block of code is the innermost set of curly brackets, so myTable can only be used up until the end of the else block.

If you want it visible after the else block, you'd need to delare it before your if. Something like:

Table myTable = new Table(a,b);
if (some condition) {
    // code removed
} else default to 20, 10, and fill randomly 1/4 {
    // code removed
}
// myTable now visible here and until the end of this block

respondido 10 mar '12, 14:03

Read about scoping of variables, and blocks; the problem is that you declared your variable inside a block, and try to refer it outside that block, making it out-of-scope and therefore not recognized.

respondido 10 mar '12, 14:03

this is a comment, not answer. - Juvanis

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.