Avatar of gudii9
Flag for United States of America

asked on 

static class


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 ;
	/*public static void main(String[] args) {
		// TODO Auto-generated method stub



Open in new window

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
JavaJava EEProgramming Languages-OtherProgrammingProgramming Theory

Avatar of undefined
Last Comment

8/22/2022 - Mon