在最近看书的过程中,常常遇到static、final、this这三个关键字,不是很明白它们的使用,查阅资料结合实例做了如下总结:

一、static——无需创建对象就可以调用(方法、属性)。

1.静态变量:static修饰的属性,称为类属性,即全局变量。前面已经有提及。

(1).静态变量可以使用类名直接访问,也可以使用对象名进行访问。

 class Number
{
int a;
static int b;
}
public class T01
{
public static void main(String[] args)
{
Number.a=5;//错误的
Number.b=6;//正确的,b是static修饰的静态变量可以直接用类名调用
}
}

(2).static不允许修饰局部变量:

 public static void max(int a,int b)
{
static int result;//Java中不被允许
}

(3).非静态和静态方法中均可以访问static变量。

2.静态方法:static修饰的方法,称为类方法。

(1).静态方法中可以调用类中的静态变量,但不能调用非静态变量。

 class Number
{
int a;
static int b;
public static void max(int c,int d)
{
int result;
result=a+b;//a是非静态变量不能被静态方法直接调用 }
}

那么如何在静态方法中调用非静态变量呢?

 class Number
{
int a;
static int b;
public static void max()
{
int result;
Number num = new Number();
num.a=5;
result=num.a+b;//借助对象调用非静态变量a
System.out.println(result);
}
} public class T01
{
public static void main(String[] args)
{
Number.b=6;
Number.max();
}
}

(2).static方法没有this—不依附于任何对象。

 class Person
{
static String name;
static int age;
public static void print(String n,int a)
{
this.name=n;//错误的,静态方法中无法使用this
age=a;
}
}

3.静态初始化块—类加载时执行,且只会执行一次,只能给静态变量赋值,不能接受任何参数。

 class Number
{
static int a;
static int b;
public static void print()
{
int result;
result=a+b;
System.out.println(result);
}
static//static语句块用于初始化static变量,是最先运行的语句块
12 {
13 a=5;
14 b=8;
15 }
}
public class T01
{
public static void main(String[] args)
{
Number.print();
}
}

因为static语句块只会在类加载时执行一次的特性,经常用来优化程序性能。

二、final关键字——“只读”修饰符
1.final和static一起使用,声明一个常量。
public static final int Age=10;//常量名大写
final成员变量必须在声明的时候初始化或在构造方法中初始化,不能再次赋值。
final局部变量必须在声明时赋值。
2.final方法,这个方法不可以被子类方法重写。

 class Person
{
public final String getName
{
return name;
}
}

3.final类,这种类无法被继承。

final class Person
{
}

4.final类不可能是abstract的。
5.final和static一样,合理的使用都可以提高程序性能。

三、this关键字——代表当前对象,封装对象属性

1.使用this调用本类中的成员变量。

public void setName(String name)
{
this.name=name;//类似于指针的作用
}

2.当成员变量和方法中的局部变量名字相同时,使用this调用成员变量。

3.this在方法内部调用本类中的其他方法。

 class Person
{
int age;
public void setAge(int age)
{
this.age=age;
this.print();//调用本类中的print()方法
}
public int getAge()
{
return age;
}
public void print()
{
System.out.println("我今年是:"+age+"岁");
}
}
public class T01
{
public static void main(String[] args)
{
Person p = new Person();
p.setAge(12);
}
}

4.this在方法中调用类的构造函数,必须位于第一行。

this(12,"Mary");//位于第一行

5.System.out.println(this);//输出对象的地址

以上的内容简单的概括了static、final、this这三个关键字的大概使用,在今后的学习中会再次进行补充。

至于super关键字将会在接下来的继承中介绍。

最新文章

  1. 三星s4宣传片配色有惊喜
  2. Spring_手动获取Bean
  3. HDU 4864 Task (贪心+STL多集(二分)+邻接表存储)(杭电多校训练赛第一场1004)
  4. WPF之外观模式
  5. NUTCH Exception in thread "Thread-12751" java.lang.OutOfMemoryError: PermGen space
  6. 解决IE浏览器IFrame对象内存不释放问题
  7. Communication System
  8. ORACLE导入导出操作篇
  9. 防止自己的网站被别人frame引用造成钓鱼
  10. R与数据分析旧笔记(十二)分类 (支持向量机)
  11. Linux的inode的理解 [转]
  12. HTML5 进阶系列:indexedDB 数据库
  13. Kotlin——最详解的类(class)的使用
  14. python 生成html文件(表格)
  15. Vue(小案例_vue+axios仿手机app)_购物车
  16. yield(),wait(),sleep(),join()
  17. 关于常用的编码工具如何引入jar包
  18. CentOS 7 Shell脚本编程第九讲 read命令简单介绍
  19. 转: rem与px的转换
  20. Linux中Centos7下安装Mysql(更名为Mariadb)

热门文章

  1. python 实现线程之间的通信
  2. POJ 2418 简单trie树
  3. SpringMVC参数绑定(二)
  4. Android 清空缓存
  5. jQuery顺序加载图片(初版)
  6. 我的web前端自学之路-心得篇:我为什么要学习web前端?
  7. 【sqli-labs】 less17 POST - Update Query- Error Based - String (基于错误的更新查询POST注入)
  8. react-draft-wysiwyg富文本
  9. 与Java注释相关的一些知识
  10. 分享接口管理平台 eoLinker AMS 线上专业版V3.0,只为更好的体验,了解一下?