java-basic knowledge

Introduction

This essay is about JavaSE basic knowledge, while I’m prepare an interview for my next job opportunity. Hence, I wrote it to review for myself. All the content is short summary from 《Core Java》, Cay S. Horstmann

Final Instacne Fields

You can define an instance field as final. Such a field must be initialized when the object is constructed. Afterwards, the field may not be modified again.
here is simple sample.

class sample{
	
	private final String sample_name;
	
}

The final modifier is particulary useful for fields whose type is primitive or an immutable class. A class is immutable if none of its methods ever mutate its objects. eg. the String class is immutable

Static Keyword

here is for useage of static.

Static Fields

If you define a field as static, then there is only one such field per class. In contrast, each object has its own copy of nonstatic instance fields.

here is simple sample

class sample{
	//All objects from this class shared same field variable.
	private static int nextId = 1;

}

Static Constants

We can use static combined with final to construct a static constants.

public class Math{

	public static final double PI = 3.141519;
}

You can access this constant in your programs as Math.PI.

Static Methods

Static methods are methods that do not operate on objects.

Math.PI();

Factory methods

A common useage of static methdos is for factory methods. A static factory methods that construct objects.

Inheritance

When designing classes, you place the most general methods in the superclass and more specialized methods in its subclasses.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值