Solved

static class

Posted on 2016-08-04
3
52 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 26

Expert Comment

by:dpearson
Comment Utility
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
Comment Utility
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 26

Accepted Solution

by:
dpearson earned 500 total points
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wordappend challenge 8 83
bitbucket vs gitbucket 3 29
JList custom Cell Renderer refresh 15 39
Base1 Encode/Decode 3 0
This is about my first experience with programming Arduino.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now