kkk
kkk

Reputation: 467

How to return statement in java method?

my situation here is i cant return my statement for my method because my method cant identify my return statement.

public static String j(){

 try{
    String k ="10";
    System.out.println(k);
    }

 catch (Exception ignore) {}

 return k; // error: cannot find symbol

}

error output :

cannot find symbol
symbol:   variable k
location: class DatabaseSQL

But if i put my return statement at try{} it will return "missing return statement"

public static String j(){ //missing return statement

 try{
    String k ="10";
    System.out.println(k);

    return k; 
    }

 catch (Exception ignore) {}

}

ANSWER

 public static String j(){
 String k ="10";// put String k before and outside try{}

 try{

    System.out.println(k);

    return k; 
    }

 catch (Exception ignore) {}

}

Upvotes: 1

Views: 414

Answers (10)

Stefan Freitag
Stefan Freitag

Reputation: 3608

According to your code the method returns a value of type String.

public static String j(){ 

You have to ensure that the return value is known for each possible execution path. If everything is ok (try-block completed w/o error), k is returned. But in case of a failure (catch-block is executed) the return value for the method is undefined, because there is no specification of the value in or after the catch-block.

Upvotes: 3

Abimaran Kugathasan
Abimaran Kugathasan

Reputation: 32458

Define like below, In First case, your local variable k type of String has a scope withing the try block

public static String j(){
 String k ="10";
 try{
    System.out.println(k);
    }

 catch (Exception ignore) {}

 return k;    
}

In second case, You haven't return anything, if an Exception caught. You either rethrow the Exception, or return something meaninfully.

Upvotes: 4

brimborium
brimborium

Reputation: 9512

This is because k is not defined in the scope of the return line. You can change it like this:

public static String j(){ //
    String k = "10";
    try {
      System.out.println(k);
    } catch (Exception ignore) {}
    return k; 
}

If your string assignment should be within the try block, give it some default value during declaration (I would recommend null) and then reassign it in the try block.

Upvotes: 3

Henrik
Henrik

Reputation: 1807

The problem is because k is not defined outside try and catch. Thats why its only working if you return in the try block if you also return null at the bottom that will be the return value of it cant return in the try block.

Do this instead and it'll work:

 public static String j(){
  String k = "";

  try{
     k ="10";
     System.out.println(k);
     }

  catch (Exception ignore) {}

  return k;

}

Upvotes: 4

Alya'a Gamal
Alya'a Gamal

Reputation: 5638

This is because you define String k in try block so it will be local variable cannot be access outside this block

and in the second code you wrote return statement in try block and there is no another one on catch so you got this error : "missing return statement"

so the true code need to be like this :

public static String j(){ 
   String k ="10";
   try{
     System.out.println(k);
     }
   catch (Exception ignore) {}

return k; 
 }

Upvotes: 4

Paulo Bu
Paulo Bu

Reputation: 29794

On your fist try, k in the return statement is out of the scope where it is defined (inside the try block).

On your second try, not all code paths in your method returns a value. Your best bet is define k outside the try statement and return it at the end.

public static String j(){
    String k ="10";

     try {
        System.out.println(k);
    }    
    catch (Exception ignore) {}
    return k;     
}

Upvotes: 3

Tim B
Tim B

Reputation: 41168

This is about variable scoping. A variable declared inside a block is only visible inside that block. Try:

public static String j(){
  String k = null;
  try{
     k ="10";
     System.out.println(k);
  }

  catch (Exception ignore) {}

  return k;

}

Note that you should never just silently eat exceptions. I know this is just a bit of test code but get out of that habit as soon as possible and you will save yourself a lot of pain in the long run!

Upvotes: 7

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35547

try{
    String k ="10"; // k declare inside the try and it will visible inside try
    System.out.println(k);
    }

 catch (Exception ignore) {}

 return k; // error: cannot find symbol // k not visible out side the try

}

You can try this way

String k ="";
try{  
 k="10";
 System.out.println(k);
} catch (Exception ignore) {}

 return k; 
}

Upvotes: 4

Salah
Salah

Reputation: 8657

You need to declare the String variable in the method scope like:

public static String j(){
   String k = "";
   try{
       k ="10";
       System.out.println(k);
    } catch (Exception ignore) {}

    return k;
}

Upvotes: 4

Baby
Baby

Reputation: 5092

Declare your String k outside try block

public static String j(){
    String k="";
    try{
        k ="10";
        System.out.println(k);
    }
     catch (Exception ignore) {}
    return k; 
}

Upvotes: 5

Related Questions