Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you initialize a String in java? [closed]

Tags:

java

string

I have taken to using setters instead of putting arguments into the default constructor because it helps me organise my code better

The problem is that the only variable on a project I am doing is a String and I am not sure whether I should be initializing it in the declaration (as a global variable?), in an setter instance method or whether to initialise it in the class constructor.

I am wondering if there could be anything problematic about this set up whether the instance is not initialised until it's setter is used:

class MyClass{

  private String myString; 

  public MyClass(){

  }
  public void setStuff(String s){ 

    this.myString=s;
  }
}
like image 303
Magpie Avatar asked Sep 26 '12 19:09

Magpie


1 Answers

Why not just initialize as:

private String myString = "My string";

The easiest way possible.

like image 149
Malcolm Avatar answered Sep 30 '22 01:09

Malcolm