java - How to use an object reference in a whole class -


so example in switch statement "case 1" declare object reference variable, good, if try use in "case 2" says reference variable cannot resolved.

how can use in every case?

edit:

switch(choice){                  case 1: {                     if(hotelobj.getclassicroomsavailable() == 0 && hotelobj.getexecutiveroomsavailable() == 0){                         system.out.println("sorry, there no available rooms");                         break;                     }else {                         scanner scaninput = new scanner(system.in);                         system.out.print("\nenter desired room type: ");                         system.out.print ("\nenter \"classic\" classic type room, price: 90$ day");                         system.out.println("\nenter \"executive\" executive type room, price: 150$ day");                         string roomchoice = scaninput.nextline();                          system.out.print("enter name: ");                         string clientname = scaninput.nextline();                         system.out.print("enter how many days you'll stay:");                         int stayingdays = scaninput.nextint();                          client clientobj = new client(clientname, roomchoice, stayingdays);                          client.clientcount++;                             if(roomchoice.equals("classic")){                                 clientobj.clientroom = new room("classic");                                 clientobj.setmoney(clientobj.getmoney()- stayingdays * clientobj.clientroom.getprice());                                 hotelobj.decclassicrooms(1);                                 hotelobj.addincome(stayingdays*clientobj.clientroom.getprice());                             } else {                                 clientobj.clientroom = new room("executive");                                 clientobj.setmoney(clientobj.getmoney()-stayingdays * clientobj.clientroom.getprice());                                 hotelobj.decexecutiverooms(1);                                 hotelobj.addincome(stayingdays*clientobj.clientroom.getprice());                             }                                         }                     break;                 }                 case 2: {                    system.out.println("name: "+clientobj.getname());                    //error "clientobj cannot resolved"                }             } 

variables declare inside case statements local statement, so, right-o, won't seen outside it. declare variable before (above) switch() , it'll visible them all.

edit: example in response brian roach below:

public void main(string[] args) {              int foo = 11;              switch (foo) {             case 1: {         int bar = 12;         system.out.println("1");         break;     }     case 2: {         system.out.println("2");         system.out.println("bar: " + bar);         break;     }     default: {         system.out.println("default");         break;     }      } 

compiler complains: "bar cannot resolved variable"

to fix, move declaration of bar same location declaration of foo.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -