?
Solved

static class

Posted on 2016-08-04
3
Medium Priority
?
133 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 28

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 28

Accepted Solution

by:
dpearson earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 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