Static and non-Static : 

非静态方法(不带static)可以访问静态变量和方法(带static),但是反过来就不行。

类的静态成员(变量和方法)属于类本身,在类加载的时候就会分配内存,可以通过类名直接去访问

非静态成员(变量和方法)属于类的对象,只有在类的对象产生(创建实例)的时候才会分配内存,然后通过类的对象去访问

在一个类的静态成员中去访问非静态成员之所以会出错是因为在类的非静态成员不存在的时候静态成员就已经存在了,访问一个内存中不存在的东西会出错。

静态变量和方法既可以用类来调用,也可以用对象来调用,但不建议用对象调用

Code :

public class Car {

		// GLOBAL VARIABLE
String mod;
int price;
static int wheels = 4; // static functions can only access static stuff
public static void main(String[] args) {
Car c1 = new Car();
c1.mod = "Merc";
c1.price = 8909809;
c1.start();
c1.accel(); Car c2 = new Car();
c2.mod = "Maruti";
c2.price = 8909809;
c2.start();
c2.accel(); System.out.println(c1.mod);
System.out.println(c2.mod); // static
System.out.println(wheels);
System.out.println(Car.wheels); c1.wheels = 8;
System.out.println(wheels);
System.out.println(Car.wheels);
System.out.println(c2.wheels); fillGas(100);
Car.fillGas(200);
c1.fillGas(900);
} public void start(){
System.out.println(mod + " starting");
} public void accel(){
System.out.println(mod + " acc");
} public static void fillGas(int quantity){ }
}

Result :

Merc starting
Merc acc
Maruti starting
Maruti acc
Merc
Maruti
4
4
8
8
8

Code :

public class Car {

		String mod;

		public static void main(String[] args) {
Car a = new Car();
Car b = new Car();
Car c = new Car(); a.mod = "A";
b.mod = "B";
c.mod = "C"; System.out.println(a.mod);
System.out.println(b.mod);
System.out.println(c.mod); a=b;
b=c;
c=a; System.out.println(a.mod);
System.out.println(b.mod);
System.out.println(c.mod);
}
}

Result :

A
B
C
B
C
B

[Training Video - 3] [Java Introduction] [Object Oriented Programming]的更多相关文章

  1. [Training Video - 2] [Java Introduction] [Operator, Loops, Arrays, Functions]

    Operator : While Loop : For Loop :  Arrays : Code : public class FirstJavaClass { public static void ...

  2. [Training Video - 2] [Java Introduction] [Install Java and Eclipse, Create Class]

    Download Java : https://java.com/en/download/ Download Eclipse : https://www.eclipse.org/downloads/ ...

  3. Object Oriented Programming python

    Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...

  4. JavaScript: Constructor and Object Oriented Programming

    Constructor :  Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...

  5. 面对对象编程(OOP, Object Oriented Programming)及其三个基本特性

    一千个读者,一千个哈姆雷特.对于面对对象编程,书上都会告诉我们它有三个基本特性,封装,继承,多态,但谈起对这三点的见解,又是仁者见仁智者见智,感觉还是得多去编程中体验把 . 面向对象编程(OOP, O ...

  6. Java - 面向对象(object oriented)计划 详细解释

    面向对象(object oriented)计划 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24058107 程序包括 ...

  7. python, 面向对象编程Object Oriented Programming(OOP)

    把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数 ...

  8. Python 面向導向語言 Object Oriented Programming Language

    Pytho 是面向對象的程式語言,舉凡 Literals 值都是 Object.例如: >>> id(38)8791423739696 與 >>> id('ABC' ...

  9. leetcode@ [355] Design Twitter (Object Oriented Programming)

    https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...

随机推荐

  1. USB gadget学习笔记

    1.usb-OTG-ADP-HNP-SRP https://blog.csdn.net/xiongjiao0610/article/details/44150849

  2. Kubernetes才是微服务和DevOps的桥梁

    一.从企业上云的三大架构看容器平台的三种视角 一切都从企业上云的三大架构开始. 如图所示,企业上的三大架构为IT架构,应用架构和数据架构,在不同的公司,不同的人,不同的角色,关注的重点不同. 对于大部 ...

  3. linux下配置python环境 django创建helloworld项目

    linux下配置python环境 1.linux下安装python3 a. 准备编译环境(环境如果不对的话,可能遇到各种问题,比如wget无法下载https链接的文件) yum groupinstal ...

  4. PHPstorm配置PHPunit对composer引入的php代码进行单元测试

    1. 如何安装PHPunit,这里不展述(如需打断点debug测试,安装PHP的xdebug扩展方法也不展开说了 https://xdebug.org/) 2.如何进行配置 以 PHP设计模式的代码为 ...

  5. SQL中減少日志文件大小

    SQL中減少日志文件大小   编写人:CC阿爸 2014-6-14 在日常SQL数据库的操作中,常常会出现SQL日志文件超大,大小都超过正常MDF数据库文件,作为一般用户来讲,LDF太大,只会影响服务 ...

  6. android studio安装须知

    64位linux,默认会提示mksdcard错误什么的,需要安装一个库 sudo apt- android sdk的下载,自己找代理服务器吧,哎……

  7. vagrant 相关记录

    最近安装vagrant 出错的最大的可能性是BOX 路径不太对, 好像和目录的大小写有关系,请检查 $ vagrant init # 初始化$ vagrant up # 启动虚拟机$ vagrant ...

  8. bzoj 3159: 决战

    Description 树上链翻转,链加,查询链上的和/max/min 树链剖分套treap,修改查询可以用类似线段树的写法,翻转可以利用分裂合并和翻转标记实现,时间复杂度O(nlog2n) 实测除了 ...

  9. [C#][控件]WebBrowser 使用范例

    if (webInfo.Document != null) webInfo.Document.OpenNew(true); else webInfo.Navigate("about:blan ...

  10. Centos生成SSL证书的步骤

    1.yum install openssl安装openssl组件2.生成KEY的流程步骤如下 1. 创建根证书密钥文件(自己做CA)root.key: openssl genrsa -out root ...