2

I've got a method that creates a String and another method that changes Strings

void create(){
    String s;
    edit(s);
    System.out.println(s);
}

void edit(String str){
    str = "hallo";
}

My compiler says that it "may not have been initialized".

Can someone explain this?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Anton Kan
  • 123
  • 1
  • 8
  • Java is pass-by-value, you do not assign string to `s` inside `bearbeiten` method. You assign it to its local `str` variable so `s` remains uninitialized. – Pshemo Jan 03 '14 at 01:49
  • Change it to `String s = null;` because, you know, `s` might not be initialized. `str = hallo` won't modify the original `s` variable. – Jeroen Vannevel Jan 03 '14 at 01:49
  • In a nutshell, variables inside methods should be initialised within the same method. On the other hand, fields / class variables can not be explicitly initialised since they will get the default value. – Ian Jan 03 '14 at 01:50
  • FYI You cannot change a string parameter value. Strings are immutable. Change edit() to a function and return a new value. – OldProgrammer Jan 03 '14 at 01:50
  • I dont really understand why I cannot change a value of a reference. Strings are no primitive types as I thought so why should I return the value and not access to its source directly? – Anton Kan Jan 03 '14 at 01:53
  • @AntonKan: because there is no pass-by-reference in Java, even though it's a reference type. More information [here](http://stackoverflow.com/questions/40480/is-java-pass-by-reference). – Jeroen Vannevel Jan 03 '14 at 01:56
  • @AntonKan `str` is like a local variable while you're inside `edit`. When you first call `edit`, `str` and `s` will be references to the same `String`. When you assign `str`, the "local variable" `str` will now be a reference to a different `String`, but that has no effect on `s`. – ajb Jan 03 '14 at 01:57
  • @Bhavik: please don't remove the question. Even though the question in itself doesn't hold much value, it does make clear where the problem is located. – Jeroen Vannevel Jan 03 '14 at 02:08
  • @AntonKan Strings are inmutable cause it hasn't setter methods you can't change it's internal structure, – nachokk Jan 03 '14 at 02:15
  • @OldProgrammer You can using reflection, although I suppose that is a different story. – Josh M Jan 03 '14 at 02:15

4 Answers4

3

Variable may not have been initialized

As you define the s inside a method you have to init s in it somewhere every variable in a program must have a value before its value is used.

Another thing not less important, your code won't never work as you expected cause Strings in java are inmutable then you cannot edit your String, so you should change your method edit(Str s).

I Change your code to something like this but i think your edit method should do another thing rather than return "hallo".

void create(){
    String s=null;
    s =edit(); // passing a string to edit now have no sense
    System.out.println(s);
}
// calling edit to this method have no sense anymore 
String edit(){
    return "hallo"; 
}

Read more about that java is passed by value in this famous question : Is Java "pass-by-reference"?

See this simple Example showing that java is passed by value. I cannot make an example with only Strings cause Strings are inmutable. So i create a wrapper class containing a String that is mutable to see differences.

public class Test{

static class A{
 String s = "hello";

 @Override
 public String toString(){
   return s;
 }

}

public static void referenceChange(A a){
    a = new A(); // here a is pointing to a new object just like your example
    a.s = "bye-bye";
}

public static void modifyValue(A a){
   a.s ="bye-bye";// here you are modifying your object cuase this object is modificable not like Strings that you can't modify any property
}

public static void main(String args[]){
   A a = new A();
   referenceChange(a);
   System.out.println(a);//prints hello, so here you realize that a doesn't change cause pass by value!!
   modifyValue(a);
   System.out.println(a); // prints bye-bye 
}


}
Community
  • 1
  • 1
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • 1
    Could you incorporate the pass-by-value, pass-by-reference difference in your post? Once that is done we might finally have an answer worth upvoting. – Jeroen Vannevel Jan 03 '14 at 02:14
  • 1
    You'll need more than just a link if you want me to take off my clothes and show you my upvote. Can you expand on how it applies to his problem? – Jeroen Vannevel Jan 03 '14 at 02:18
  • @JeroenVannevel i edit with a custom example , showing differences – nachokk Jan 03 '14 at 02:25
  • 1
    Thank you very much, I fear I just mixed up pointer of C with references of Java ;) But your example helped me getting it! – Anton Kan Jan 03 '14 at 11:30
-1

You declare local variable s in method create, so that you need to initialized it before you use it. Remember that java does not have default value for local variable. Init String s = "" or whatever value than your code will run normally.

linhln
  • 1
  • 1
-3

try to initialize the string "s" to a null value, since you have declared a variable "s" but it has not been initialized. Hence it can't pass the reference of that variable while used as parameter.

String s = null;

Hope this helps

Anuragh27crony
  • 2,957
  • 1
  • 19
  • 29
-3

Give your variable S a value or as Jeroen Vanneve said "Change it to String s = null;"