?
Solved

static class

Posted on 2016-08-04
3
Medium Priority
?
109 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
Introduction to Processes
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 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