moocNotes-Java object-oriented programming(static)

Why we need static?

We have konwn new can create a new object and system will locate new memory to this object, but sometimes we hope some data can keep unchange, which means we only need one memory to store this attribute.

eg, all students have the same school name, so ,when all student in one school will only need one school name. How to make all students share the school name?

  1. static can refer variable or method which do not rely on specific object

  1. System will only allocate memory when create the first object. which means if we new another object, system will not allocate memory for this static variable or staic method.And all object share the memory.

  1. the static member can be called by class name or object name.

  1. the lifelong of static is same with class.

  1. 静态方法不能直接访问所属类中的非静态变量和非静态方法

    public static String schoolName = "SD";

    System.out.print(james.schoolName);
    System.out.print(Student.schoolName);

解释main 方法

public static void main(String[] args)
  • public:由于JVM需要调用main方法,因此该方法的权限必修是public.

  • static:main方法是静态方法,JVM创建main方法的时间较早,不依赖于对象。main方法不能直接访问类中的非静态成员,因此需要创建实例对象,通过对象访问非静态成员。JVM 在执行main方法时,不必创建对象,所以方法必须是static.

  • void 没有返回值

  • String[]:该数组保存执行java命令时传递给运行类的参数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值