Reading Effictive java: static member class (SMC) and nonstatic member class(NSC)

Misunderstanding of static member class :

For most programmers, comparing to SMC ,we may be more familiar with static field . so we may analogously guess how SMC works according to static field‘s feature. That leads to a misunderstanding.--SMC may only have one
instance.

Prove misunderstanding is wrong:

We may see how stupid this is , after you think this over. If it means client can‘t create an instance of a static class .The only way is to make all constructor  ‘private‘ or ‘package default‘,and don‘t provide any public
method which have the function of ‘create a instance‘.

So the thing is if we use ‘Static‘ to modify a inner class , it doesn‘t have any matter with whether client can create multi instances.

Application of SMC

1. SMC can work independently , you can move a SMC out of its enclosing Class. It works well.

e.g. here we try to do plus operation use Calculator:

if we move SMC out of enclosing Class:

public enum OuterOperator {
	PLUS,MINUS
}
public class Calculator {
	 static enum Operation{
	PLUS,MINUS
}

	public int operate(int a, int b,Operation o){
		switch(o){
			case PLUS:
				return a+b;
			case MINUS:
				return a-b;
			default:
				//use 0 as default value,this might be inappropriate ,just ignore
				return 0;
		}
	}

	public int operate(int a, int b,OuterOperator o){
		switch(o){
			case PLUS:
				return a+b;
			case MINUS:
				return a-b;
			default:
				//use 0 as default value,this might be inappropriate ,just ignore
				return 0;
		}
	}

	public static void main(String[] args){
		Calculator cal=new Calculator();
		int result=cal.operate(3, 1, Calculator.Operation.PLUS);
		System.out.println("Calculator operation:"+Calculator.Operation.PLUS+" result: "+result);

		int result2=cal.operate(3, 1, OuterOperator.PLUS);
		System.out.println("OuterOperator operation:"+OuterOperator.PLUS+" result: "+result2);
	}
}

result:

Calculator operation:PLUS result: 4

OuterOperator operation:PLUS result: 4

so it works well , so why bother to make class Operation a static member class. The answer is we can easily tell

Operation is a component of Calculator. So here is it, SMC are often used as an component of enclosing class.

another example is Node in HashMap(jdk1.8).

Differences between static member class (SMC) and nonstatic member class(NSC)

SMC doesn‘t need an instance of enclosing class to bind,while NSC does.

SMC

public class outClass {
    public static class innerClass{
        public innerClass(){
            System.out.println("innerClass");
        }
    }
    public static void main(String[] args)
    {
        //outClass a = new outClass();
        innerClass b = new innerClass();
        innerClass c = new innerClass();

    }

}

NSC

public class outClass {
    public class innerClass{
        public innerClass(){
            System.out.println("innerClass");
        }
    }
    public static void main(String[] args)
    {
        outClass a = new outClass();
        innerClass b = a.new innerClass();
        innerClass c = a.new innerClass();

    }

}

So comparing to SMC, NMC may spend one more reference to refer to its enclosing instance. it waste space and time. but for using SMC, InnerClass need to meet the condition -- InnerClass‘s methods don‘t need enclosing instance‘s reference.  like
methods in Entry(getKey,getValue,setValue) don‘t need to refer to Map.

时间: 2024-08-28 16:30:17

Reading Effictive java: static member class (SMC) and nonstatic member class(NSC)的相关文章

Java static 关键字

本文介绍 Java static 关键字的用法和注意点: 1. 使用 static 声明属性--即 static 声明全局属性 2. 使用 static 声明方法--即通过类名直接调用 static 方法 注意点: 使用 static 方法的时候,只能访问 static 声明的属性和方法,而非 static 声明的方法和属性是不能访问的. 我们使用如下代码声明三个对象,并且相应的进行赋值: package hello; class People2{ String name; int age; p

java static块详解

1. java static块执行时机 java static块在类被初始化的时候被执行. 参考<深入Java虚拟机>中的描述,一个java class的生命周期: 装载 通过类的全限定名,产生一个代表该类型的二进制数据流: 解析这个二进制数据流为方法区内的数据结构: 创建一个表示该类型的java.lang.Class的实例. 如果一个类装载器在预先装载的时遇到缺失或错误的class文件,它需要等到程序首次主动使用该类时才报告错误. 连接 验证,确认类型符合Java语言的语义,检查各个类之间的

JAVA static 作用

static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何对象.也就是说,它不依赖类特定的实例,被类的所有实例共享.只要这个类被加载,Java虚拟机就能根据类名在运行时数据区的方法区内定找到他们.因此,static对象可以在它的任何对象创建之前访问,无需引用任何对象. 用public修饰的static成员变量和成员方法本质是全局变量和全局方法,当声明它类的

java static那些事

 staic变量是怎么样执行的? public class Client {public static int i = 0;static {  i = 100;}public static void main(String[] args) {  System.out.println(i);}} 这段程序很简单,输出100,那么下面稍微修改下,代码如下: public class Client { static {  i = 100; } public static int i = 0; publ

Scala Java Error: value filter is not a member of *

错误字符串 Scala Java Error: value filter is not a member of * 解决办法 在代码中引入下面名字空间 import collection.JavaConversions._

Java Static关键字之小程序

对于学习static我们先来看两个例子: 1 public class Cat { 2 private static int sid = 0; 3 private String name; 4 int id; 5 Cat(String name) { 6 this.name = name; 7 id = sid++; 8 } 9 public void info(){ 10 System.out.println 11 ("My name is "+name+" No.&quo

03 Java static 和 final

Java 修饰符主要分为两类: 访问修饰符 非访问修饰符 非访问修饰符 Java static static 是一个修饰符,被 static 修饰的成员变量和成员方法,不依赖类的特定实例,被类的所有实例共享,因此静态类方法中不可以定义 this,super 等关键字. 静态成员变量和方法,可以使用类直接调用,如下可以直接使用 Dog 访问 name,age,printInfo(). 静态方法只能访问静态成员,非静态方法既可以访问静态也可以访问非静态成员,如在 printInfo() 方法使用 s

Nonstatic Member Function 的语意

C++ 的设计准则之一就是: nonstatic member function 至少必须和一般的 nonmember function 有相同的效率. 这就是说, 如果我们在以下两个函数之间做选择: float magnitude3d(const Point3d *_this){...} float Point3d::magnitude3d() const {...} 那么选择 member function 不因该有什么额外负担, 这是因为编译器内部已将 member 函数实体转换为对等的

invalid use of non-static member function ‘void Date::init(int, int, int)’

#include<iostream> using namespace std; class Date { public: int day,month,year; void init(int,int,int); void print_ymd(); }; void Date::init(int yy, int mm, int dd) { year = yy; month = mm; day = dd; } void Date::print_ymd() { std::cout << ye