When I was worried about static methods in java interface, I arrived in the order of name interpretation

Although it says interface and static method I'm not talking about ** static functions ** in interfaces implemented in java8.

Those who expected this are sorry.


The other day? I've thought that, so I'll leave it as a memo.

ArleneFamily.java


public final class ArleneFamily {

	public static final class Father {
		public static String name() {
			return "Irene's dad";
		}
		public static String birthDay() {
			return "3/2";
		}
	}

	public static final class Mother {
		public static String name() {
			return "Irene's mom";
		}
		public static String birthDay() {
			return "10/30";
		}
	}
}

Sample1.java


public class Sample1 {
	public static void main(String[] args) {
		System.out.println(ArleneFamily.Father.name() + ":" + ArleneFamily.Father.birthDay());
		System.out.println(ArleneFamily.Mother.name() + ":" + ArleneFamily.Mother.birthDay());
	}
}

There is a constant class like this ... It is the Irene family composition class (ArleneFamily.java) and the class that displays it (Sample1). Actually, it seems that there are many XX family composition classes other than the Irene family.

As an existing specification, I only displayed the configuration of each house. I want to display birthdays for both the Brett and Cindy families! !! There is something like that ...

Sample2.java


public class Sample2 {
	public static void main(String[] args) {
		System.out.println(ArleneFamily.Father.name() + ":" + ArleneFamily.Father.birthDay());
		System.out.println(ArleneFamily.Mother.name() + ":" + ArleneFamily.Mother.birthDay());
		System.out.println(BretFamily.Father.name() + ":" + ArleneFamily.Father.birthDay());
		System.out.println(BretFamily.Mother.name() + ":" + ArleneFamily.Mother.birthDay());
・
・
・
	}

It looks like this, but ** Korehanaidaro ... **

So with an interface, I decided to think that the user wants to have something like the following.

Sample3.java


	public static void main(String[] args) {
		printBirthDay(~ Moyamoya ~);
		printBirthDay(~ Moyamoya ~);
・
・
・
	}

	private static void printBirthDay(~ Moyamoya ~) {
		System.out.println(interface.name() + ":" + interface.birthDay());
	}

Immediately create a Human interface,

Human.java


public interface Human {
	public String name();
	public String birthDay();
}

impl grant.

ArleneFamily.java


public final class ArleneFamily {

	public static final class Father implement Human {
		public static String name() {
			return "Irene's dad";
		}
		public static String birthDay() {
			return "3/2";
		}
	}

	public static final class Mother implement Human {
		public static String name() {
			return "Irene's mom";
		}
		public static String birthDay() {
			return "10/30";
		}
	}
}

Yes. This is an error. I was angry that I should remove the static method.

I understand that * static cannot be overridden, but I think it's okay to write if you are compelled to implement the interface *

Since it doesn't work, remove static from the method.

ArleneFamily.java


public final class ArleneFamily {

	public static final class Father implement Human {
		public String name() {
			return "Irene's dad";
		}
		public String birthDay() {
			return "3/2";
		}
	}

	public static final class Mother implement Human {
		public String name() {
			return "Irene's mom";
		}
		public String birthDay() {
			return "10/30";
		}
	}
}

Yes, this time there is an error in the part you are using. ↓ Here part

System.out.println(ArleneFamily.Father.name() + ":" +

Create an instance! Well, that's because I removed the static.

There is no choice but to fix the side that is using it ... There are quite a few, and it will be difficult within the range of influence. When I thought, ** I was shocked! ** **

ArleneFamily.java


public final class ArleneFamily {

	public static final Human Father = new Human() {
		@Override
		public String name() {
			return "Irene's dad";
		}
		@Override
		public String birthDay() {
			return "3/2";
		}
	};

If you do it like this, you don't have to touch it, right?

If you try Sure enough, this doesn't cause an error on the user side ... ↓ This part. It goes well.

System.out.println(ArleneFamily.Father.name() + ":" +

Furthermore, since the class structure was finalized in the form of instance creation Eliminates the writing of the moody part.

Sample3.java


	public static void main(String[] args) {
		printBirthDay(ArleneFamily.Father);
		printBirthDay(ArleneFamily.Mother);
・
・
・
	}

	private static void printBirthDay(Human human) {
		System.out.println(human.name() + ":" + human.birthDay());
	}

For the time being, since it was originally a Constant class, I made the default constructor private so that it would not be new. With this, the repair of the original purpose is completed safely ~~

Well finally the main subject. (Lol)

If the above compiles, The original inner class (Father) and the variable that implements the Human interface (Father) Can you coexist? Which will be prioritized if possible? To the question ...

ArleneFamily.java


public final class ArleneFamily {

	public static final Human Father = new Human() {
		@Override
		public String name() {
			return "Irene's dad";
		}
		@Override
		public String birthDay() {
			return "3/2";
		}
	};

	public static final class Father {
		public static String name() {
			return "Irene's father";
		}
		public static String birthDay() {
			return "11/12";
		}
	}

Yes, I was able to live together. .. Change the return value and try which one is actually called.

Execution result


Irene's dad:3/2

The variable has been prioritized. The result of various investigations Here's 6.4.2. Obscuring, 6.5. Determining the Meaning of a Name It was written properly.

** Variables take precedence over types. Type takes precedence over package. ** **

I did not know··· Well, it's impossible to have the same name on site. It was known at a good opportunity this time.

By the way, I took Family as an example, but it is actually a product system. If you replace the name, it looks like this orz It feels strange.

It's a story behind java, so there is no heading.

Recommended Posts

When I was worried about static methods in java interface, I arrived in the order of name interpretation
Summary of points I was worried about when migrating from java to kotlin
[Java] I thought about the merits and uses of "interface"
About the description order of Java system properties
About the idea of anonymous classes in Java
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
When I think about the 402 error that suddenly appeared in the middle of the introduction of PAY.jp, there was an unexpected place
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
Was done in the base year of the Java calendar week
Traps brought about by the default implementation of the Java 8 interface
Think about the differences between functions and methods (in Java)
[Java] Get the dates of the past Monday and Sunday in order
Spring validation was important in the order of Form and BindingResult
I thought about the strategy of introducing Combine in iOS development
I tried to summarize the methods of Java String and StringBuilder
Access the network interface in Java
What I don't like when using interface of a function with default arguments in Kotlin from Java
Order of processing in the program
How to find the total number of pages when paging in Java
I want to get the IP address when connecting to Wi-Fi in Java
I want to get the field name of the [Java] field. (Old tale tone)
I was stuck with the handling of the time zone when formatting with SimpleDateFormat
In Apache POI 3.15, when I get the result of the formula, FormulaParseException occurs (the formula refers to "cell of sheet name including" ・ ")
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
[Java] I participated in ABC-188 of Atcorder.
Get the result of POST in Java
I tried the new era in Java
[day: 5] I summarized the basics of Java
The story of writing Java in Emacs
I received the data of the journey (diary application) in Java and visualized it # 001
Character string comparison: I was caught in the skill check problem of Paiza
[Java] I checked it because it was erratic when converting the type. <Cast operator>
About the meaning of type variables, E, T, etc. used in generics used in Java
I didn't know that inner classes could be defined in the [Java] interface
I was swallowed by the darkness of the romaji, trying to convert my name to romaji
Provisional memo when the name of the method parameter of the Java class cannot be obtained by reflection in the Eclipse plug-in project.
[Elasticsearch × Java] Since the execution result of the query acquired in Java was different from the assumption, I investigated → Summary of the corresponding contents
I want you to put the story that the error was solved when you stabbed the charger in the corner of your head
[Words spelled to me when I was in the first grade ⑩] Please understand the minimum Java coding method. (1st: Naming)