0

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

2
  • where is your main method ? Commented Dec 4, 2013 at 1:23
  • @Trojan.ZBOT there i edited my post Commented Dec 4, 2013 at 1:29

4 Answers 4

1

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 } } 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

For one, you can call

NonStaticCalling.getString2() 

but not

NonStaticCalling.getString() 

A static method can be called without instantiating the class.

2 Comments

hmm can you explain more why does it print the same? I declare a new object so why?
Based on your updated code, you are calling System.out.println() twice: once in the main method and once in the constructor. If you want to better understand which is called when you should add some distinguishing text to each (or just one) of the System.out.println() calls. As davecom and TheKojuEffect point out, the static variable is set in your main method, so it is set for all instances and/or calls to the static method no matter what NonStaticCalling instances you may or may not instantiate.
0
 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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.