Solved

static class

Posted on 2016-08-04
3
73 Views
Last Modified: 2016-08-04
Hi,

public class TestStringImmutability {

	
	public static class PrintName {
		public static void main(String[] args) {
			String greeting = "Hello my name is ";

			printMyName(greeting, "Doug") ;
			// "Hello my name is Doug"

			printMyName(greeting, "Sam") ;
			// "Hello my name is Sam"
		}

		public static void printMyName(String g, String name) {
			// Does NOT change the original greeting String - which is immutable
			g += name ;
			System.out.println(g);
		}
	}
	/*public static void main(String[] args) {
		// TODO Auto-generated method stub

	}*/

}

Open in new window


output
Hello my name is Doug
Hello my name is Sam


why i was forced to write above cass as static by compiler to compile code. I never remember writing any class as static? what is significance of static for a class? How to execute above code without static keyword. usually static used for class members to make them global right (for method and variables without need to create instance to call them). please advise
0
Comment
Question by:gudii9
  • 2
3 Comments
 
LVL 27

Expert Comment

by:dpearson
ID: 41742960
The key is that the class is inside another one:

public class TestStringImmutability {
    public static class PrintName {
    }
}

Open in new window


If you make the internal class "static" it actually (oddly enough) means make this a "normal class".  So you should use it 99% of the time.

If you don't include the keyword static then the inner class (PrintName here) retains a special pointer back to the outer class (TestStringImmutability).

You access this magic extra property like this:
TestStringImmutability.this
from within the inner class.

In my opinion, this goes under the heading of "cool ideas that were introduced into Java, that they maybe regret now".

So learn to add "static" to inner class definitions and you can safely ignore this special inner class behavior.

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 41743209
The key is that the class is inside another one:

public class TestStringImmutability {
    public static class PrintName {
    }
}

Select all
 
Open in new window

If you make the internal class "static" it actually (oddly enough) means make this a "normal class".  So you should use it 99% of the time.

ok i noticed now there are tow classes


If you make the internal class "static" it actually (oddly enough) means make this a "normal class".  So you should use it 99% of the time.

can you please elaborate on this. i was not clear. are you saying it is always good idea to make inner classes as static rather than non static?
0
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 41743331
are you saying it is always good idea to make inner classes as static rather than non static?

Yes that's right.

If you add "static" they will behave exactly like every other class you've ever encountered.
If you don't add "static" they will behave somewhat differently - with special problems and challenges.

So just add "static" and your life will be simpler.

Doug
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question