Solved

static class

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

861 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

30 Experts available now in Live!

Get 1:1 Help Now