-2

obj not recognized after declaring in switch statement

switch (var){
        case "YES" : {
              Object1 obj = new Object1();
              break;
       }
        default: {
              Object2 obj = new Object2();
       }

     }

NewObject newObject = obj.objToNewObject(param);

My problem is that "obj" is not recognized -> "Error java: cannot find symbol"

I was thinking that with "default" i will work but no. Any help ?

amalicode
  • 37
  • 1
  • 8

1 Answers1

3

The object obj in your code is a local variable of the switch statement, so it cannot be accessed outside this scope. Also, to be able to use obj as two different classes you will have to use Polymorphism, so you will have to declare a base class (I will use A to illustrate) and two different classes (B and C) that extends class A.

class A {
    public NewObject(param) {
        return new NewObject();
    }
}

class B extends A{
    @Override
    public NewObject(param) {
        return new NewObject();
    }
}

class C extends A { 
    @Override
    public NewObject(param) {
        return new NewObject();
    }
}

A obj = null;
switch (var){
        case "YES" : {
              obj = new B();
              break;
       }
        case "NO": {
              obj = new C();
       }
        default: {
              obj = new B();
       }

     }
NewObject newObject = obj.objToNewObject(param);

Note that the classes B and C will have to override the method objToNewObject from class A for this to work

Henrique Sabino
  • 546
  • 3
  • 19
  • Mh.. its differents object – amalicode May 29 '19 at 15:45
  • 2
    @amalicode what is wrong with the answer? It fully answers your question and example is given – Void Spirit May 29 '19 at 15:46
  • @amalicode Your edit changes nothing. You still have to declare outside of the switch. – zero298 May 29 '19 at 15:50
  • @amalicode this is the correct answer. What you're trying to do will not work without `Object1` and `Object2 ` sharing a common interface. If they do share a common interface, then `obj` should be declared outside of the switch statement to be an instance of the interface. – Chris Thompson May 29 '19 at 15:51
  • i read it. how chose beetween Object1 and Object2 when i do Object obj = null; – amalicode May 29 '19 at 15:51
  • @amalicode I edited the answer, hope this will help you – Henrique Sabino May 29 '19 at 15:53
  • "Method does not override method from its superclass" when i override method @HenriqueSabino – amalicode May 29 '19 at 16:19
  • it was due to "static". But why ? – amalicode May 29 '19 at 16:23
  • @amalicode make sure the methods names are the same as in the base class, also use the annotation `@Override` so then the compiler knows that it should be an overridden method, the syntax should be like I put in the edits – Henrique Sabino May 29 '19 at 16:23
  • @amalicode Static methods are class methods while non-static are instance methods, take a look at this link here https://stackoverflow.com/questions/3903537/what-is-the-difference-between-a-static-method-and-a-non-static-method – Henrique Sabino May 29 '19 at 16:24
  • @amalicode Also, read this topic here, this should also help you https://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods – Henrique Sabino May 29 '19 at 16:25