Principle

To offer the benefits of encapsulation you should always expose private field with public accessor method.

Correct Implementation

// Encapsulation of data by accessor methods and mutators

class Point {

private double x;

private double y;

public Point(double x, double y) {

this.x = x;

this.y = y;

}

public double getX() { return x; }

public double getY() { return y; }

public void setX(double x) { this.x = x; }

public void setY(double y) { this.y = y; }

}

Note

While it's never a good idea for a public class to expose fields directly, it is less harmful if the fields are immutable.

/**

* Public class with exposed immutable fields - questionable

*

* @author Kaibo

*

*/

public final class Time {

private static final int HOURS_PER_DAY = 24;

private static final int MINUTES_PER_HOUR = 60;

public final int hour;

public final int minute;

public Time(int hour, int minute) {

if (hour < 0 || hour >= HOURS_PER_DAY)

throw new IllegalArgumentException("Hour: " + hour);

if (minute < 0 || minute >= MINUTES_PER_HOUR)

throw new IllegalArgumentException("Min: " + minute);

this.hour = hour;

this.minute = minute;

}

public static void main(String[] args) {

Time t = new Time(1, 30);

t = new Time(2, 0);

System.out.println(t);

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return "Time [hour=" + hour + ", minute=" + minute + "]";

}

}

Effective Java 14 In public classes, use accessor methods, not public fields的更多相关文章

  1. 读Effective Java笔记之one:static Factory methods instead of Constructors (静态工厂方与构造器)

    获取类的实例的方法有很多种,在这很多种方法中,它们各有优缺,各有特点.这里,只介绍2中方法 1.使用构造方法 public class Person { private String sex; /** ...

  2. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  3. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  4. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  5. 《Effective Java》读书笔记 - 4.类和接口

    Chapter 4 Classes and Interfaces Item 13: Minimize the accessibility of classes and members 一个好的模块设计 ...

  6. Effective Java 13 Minimize the accessibility of classes and members

    Information hiding is important for many reasons, most of which stem from the fact that it decouples ...

  7. Effective Java 第三版——14.考虑实现Comparable接口

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. Effective Java Chapter4 Classes and Interface

    MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...

  9. Effective Java 第三版——16.在公共类中使用访问方法而不是公共属性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

随机推荐

  1. SQL Server里强制参数化的痛苦

    几天前,我写了篇SQL Server里简单参数化的痛苦.今天我想继续这个话题,谈下SQL Server里强制参数化(Forced Parameterization). 强制参数化(Forced Par ...

  2. SQL 表变量和临时表

    SQL 表变量和临时表 表变量:存储在内存中,作用域是脚本的执行过程中,脚本执行完毕之后就会释放内存,适合短时间内存储数据量小的数据集. 优点:使用灵活,使用完之后立即释放,不占用物理存储空间 缺点: ...

  3. 移动端手势库Hammer.js学习

    感觉移动端原生支持的 touch.tap.swipe 几个事件好像还不够用,某些时候还会用到诸如缩放.长按等其他功能. 近日学习了一个手势库 Hammer.js,它是一个轻量级的触屏设备手势库,能识别 ...

  4. 关于iPhone多点触控

    虽然这个问题很简单,但是对于我这接触两天的菜鸟来说也弄了很久,网上又找不到相关的解决方法,避免其他人和我一样,还是记录一下 一般网上找到的教程是这么教: -(void )touchesBegin:(N ...

  5. ASP.Net中Session失效的一种编程思路

    在写一个客户的B/S结构应用程序时,突然发现一个技巧,不知道是否是MS的一个BUG,给相关的有研究的朋友原先考虑写一个检查Session的类,Session失效后,必须转向登陆页面,可每一个调用该类的 ...

  6. csharp: Flash Player play *.flv file in winform

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. 批量导数据之利器-load data[2016-07-11]

    由于天热,中午吃完饭后不再去逛了,感觉这段时间其实也是可以利用起来的,所以决定每天中午积累一些小的知识点.今天中午,先总结一下最近造数据用到手命令,load data. 使用这个命令的起源是因为最近要 ...

  8. NYOJ:题目524 A-B Problem

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860 My思路: 先用两个字符串储存这两个实数,然后再用另外两个字符串储存去掉符号和前后多 ...

  9. Mysql denied for user 'odbc@localhost' || denied for user 'root@localhost'

    1. Question Description: 1.1  mysql  version:  mysql-5.7.11-win64.zip 1.2  if you connect to the mys ...

  10. Maven多模块项目使用MyBatis Generator

    开发环境: JDK:8u102 Maven:3.3.9 MySQL:5.7.10 MySQL Connector:5.1.40 IDE:IntelliJ IDEA 2016 MyBatis:3.4.1 ...