user31546
user31546

Reputation: 13

Static method vs non static method java better understanding

I still cannot fully understand this static and non static.

public static void  main(String args[]){

    staticClass.setString("hey there");
    System.out.println(staticClass.getString2());
    //expecting to be blank
    NonStaticCalling nonStaticCalling = new NonStaticCalling();
}

static String aw = "";
public static void setString(String a){
    aw =a;
}

public String getString(){
    return aw;
}

public static String getString2(){
    return aw;
}


public class NonStaticCalling {
    staticClass staticClass = new staticClass();
    public NonStaticCalling(){
        staticClass.getString();
        System.out.println(staticClass.getString());
    }
}

If i understand correctly. I declare a new object nonstaticcalling. So i assume that the value of the output from that class is "" (blank)

Can someone give me a better exmaple? thanks

Upvotes: 0

Views: 149

Answers (4)

Alejandro
Alejandro

Reputation: 1159

 SomeName.setString("hey there");
        System.out.println(SomeName.getString2());

//expecting to be blank
        SomeName object = new SomeName();
 object.setString2("hey there");
System.out.println(object.getString());


public class SomeName
{
    static String aw = "";
    String aw2 = "";

    public SomeName()
    {

    }
    public static void setString(String a){

        aw =a;
    }
   public void setString2(String a){

        aw2 =a;
    }

    public String getString(){

        return aw;
    }

    public static String getString2(){
        return aw;
    }
}

This will print what you got! so the difference is that in one you are using a static property of the class, this means that if you change it, it changes for every other object using it in the future!

In the second one you are using an "object" or an instance of the class, this means that all variables are only set to that object while it lives! If you create a new one you will have to set up aw2 again for it!

Upvotes: 0

TheKojuEffect
TheKojuEffect

Reputation: 21081

Static variables are created only one for all the objects of that StaticClass so you're return the same static variable from newly created object.

Upvotes: 1

davecom
davecom

Reputation: 1498

When a static variable is set, it is the same for all instances of the class. Static variables are also known as "class variables". I think your confusion is actually about the variable more so than the methods. Take this example with no static variables as a simple example. "name" is the same for all instances of the class "myName" (sorry should've made it capital since it's a class name).

public class myName {
  public static String name;
  public void setName(String newName) {
    name = newName;
  }
  public String getName() {
    return name;
  }
  public static void main(Strings args[]) {
     myName first = new myName();
     myName second = new myName();
     first.setName("hello");
     System.out.println(second.getName());  //prints hello
  }
}

Upvotes: 1

eebbesen
eebbesen

Reputation: 5148

For one, you can call

NonStaticCalling.getString2()

but not

NonStaticCalling.getString()

A static method can be called without instantiating the class.

Upvotes: 0

Related Questions