0

Given the code below:

class Demo {                    
    static String s = "123";    

    static void m1(String s) {
        this.s = s;             
    }

    void m2(String s) {
        this.s = s;              
    }
}

class Hello {
    public static void main(String args) {
        Demo.m1("456");          
        Demo d = new Demo();       
        d.m2("789");         
    }
 }

I would like to know what is the difference between using an instance variable and a static variable regarding object creation:

  1. Given that string is immutable and variable s is static, how many objects are created for variable s?

  2. Is a new object created when a static method such as m1() is called?

  3. Is a new object created when an instance method such as m2() is called?


Edited:

I had a wrong assumption about static keyword. Now I am clear about it.

  1. static is a keyword.
  2. It is used for declaring the members of the class.
  3. static members belong to class.
  4. Instance variables cannot be used in static context.
  5. this keyword cannot be used in static context.
  6. static members can be accessed without creating an object.
  7. Static variables come to life when class is loaded into the memory.
  8. this keyword can be used only to access instance members.
  9. this keyword is used to access the members of the class within the same class.

Thanks for helping. Below is the edited code:

class Demo {                    
    static String s= "123";  //Static variable
    String s1 ="abc"; // Instance variable  

    static void m1(String s) {
        Demo.s = s; //Accessing an static variable       
    }

    void m2(String s,String s1) {
        Demo.s = s; 
        this.s1 = s1;//Accessing an instance variable           
    }
}

class Hello {
    public static void main(String args[]) {
        Demo.m1("456");          
        Demo d = new Demo();       
        d.m2("789","xyz");         
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Vinay S Jain
  • 111
  • 1
  • 11
  • And what do you personally think about this questions? – Andremoniy Mar 19 '15 at 16:35
  • I am bit confused about static variable.if it was a instance variable then new object would have created.because String is immutable. – Vinay S Jain Mar 19 '15 at 16:39
  • Please keep in mind: `s` is not an instance of String object - it's a variable. In Java a variable is a reference to object instance, not an instance itself. `"123"` is a string literal, and it represents a single instance of String object. `s="123"` is setting the reference `s` to point to the string instance containingcharacters 1,2 and 3. –  Mar 19 '15 at 16:45

2 Answers2

4

The static modifier just means that a variable is a member of the class, and not of a specific instance. It has no relation to whether a new object is being created or not.

First some observations about your code:

The method m1():

static void m1(String s) {
    this.s = s;
}

This won't compile, because you cannot reference an instance variable from a static method. It should be:

static void m1(String s) {
    Demo.s = s;
}

The method m2():

void m2(String s) {
    this.s = s;
}

Now this won't give you a compile error, but should give you a warning. this should be used only for instance attributes and methods. Since s is a class attribute, you should use the name of the class instead:

void m2(String s) {
    Demo.s = s;
}

Now we can reason about the code:

  1. When you do static String s = "123", you create a new String and make s point to it, so s -> "123".

  2. When you call Demo.m1("456"), you create another String, "456" this time, and make s point to it, so s -> "456".

  3. When you call d.m2("789"), again you create a String object, "789", and make s point to it, so s -> "789".

So, in total, you created 3 String objects.

Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48
  • Thanks...Is there any difference between calling m1() method and calling m2().. – Vinay S Jain Mar 19 '15 at 17:01
  • I suggested a couple of corretions to the methods m1() and m2() in my updated answer. Given these changes, the effects of both methods should be the same. The only difference is that you need a Demo instance to call m2(). – Anderson Vieira Mar 19 '15 at 17:11
  • so there is no advantage of creating an static method...where does the static method and static variable stored..??? – Vinay S Jain Mar 19 '15 at 17:54
  • Static methods and variables should be created when you need to store and process information that is related to a class but not to any specific instance of it. For further reading and examples about static members, you can check the documentation: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Anderson Vieira Mar 19 '15 at 18:18
  • last question- An object is an instance of a class" ...can you please elaborate...instance and object difference.. – Vinay S Jain Mar 19 '15 at 18:21
  • There's no practical difference. An object is the result of a class instantiation. Check this: http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html – Anderson Vieira Mar 19 '15 at 18:32
0

Only one static String s is created (which is the basic idea behind static). So by calling Demo.m1("353") you are changing s to "353" and by creating a new Demo d, and calling d.m2("344") you are overwriting the content "353" with "344".

I am a bit confused about the question because you can easily test it yourself by using

public void talk(){
    System.out.println("my static string s is: "+s)
}

and print the s in different stages of your program. Do you want to know WHY that stuff happens?

this might help you: Immutability of Strings in Java

Community
  • 1
  • 1
meister_reineke
  • 364
  • 1
  • 14