知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

      • Theory:Introduction OOP
      • Theory:Defining classes
      • Theory:Instance methods
        • What's the difference?
        • Understanding: static and instance
        • Instance methods: features
        • Summary
      • Thoery:Constructor
      • Theory:Package
      • Theory:Access modifiers
      • Theory:Getters and setters
      • Theory:Inheritance
      • Theory:Protected modifier
      • Theory:Referencing subclass objects
      • Theory:The Object class
      • Theory:Objects
      • Theory:Interface
      • Theory:Multiple constructors
      • Theory:The keyword super
      • Theory:Static members
      • Theory:Hiding and overriding
      • Theory:Polymorphism
      • Theory:Abstract class
      • Theory:Abstract class vs interface
      • Theory:Anonymous classes
      • Theory:Anonymous classes properties
    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Java OOP
Jim
2022-05-05
目录

Theory:Instance methods

As you know, a class is one of the fundamental concepts in Java. You develop the logic of a program by creating fields and methods inside classes. Fields describe an object's properties and methods describe what an object does. In this topic, we will go on to discuss the nature of methods in Java. All methods can be divided into two groups: instance and static. We were mostly focused on the static ones previously, so now let's learn more about instance methods to better understand the differences between the two.

# What's the difference?

Let's look at the code below. Here we have a class named Human with two fields and two methods.

class Human {
    String name;
    int age;

    public static void printStatic() {
        System.out.println("It's a static method");
    }

    public void printInstance() {
        System.out.println("It's an instance method");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

Modifier public isn't important for us now. It just means that other classes can also get access to our methods.

As you see, methods printStatic and printInstance have differences in declaration. When you see a method, you can easily understand: if there is a word static, then the method is static; if there is no word static, then the method is an instance one.

Now let's see what this really means!

# Understanding: static and instance

To invoke a static method we don't need to create an object. We just call the method with the class name.

public static void main(String[] args) {

    Human.printStatic(); // will print "It's a static method"
}
1
2
3
4

In other words, you can say that a static method belongs to a class (because we don't need an object).

The instance method requires a different invocation. As you already guessed, to invoke an instance method we have to create an object first. Otherwise, there is no way to use an instance method.

It's called the instance method because an instance is a concrete representation of an object.

Here we call the method printInstance for two different objects:

public static void main(String[] args) {
        
    Human peter =  new Human();
    peter.printInstance(); // will print "It's an instance method"

    Human alice =  new Human();
    alice.printInstance(); // will print "It's an instance method"
}
1
2
3
4
5
6
7
8

So, we can say that an instance method is a method that belongs to each object that we created of the particular class.

# Instance methods: features

Instance methods have a great advantage: they can access fields of the class.

To illustrate the feature, let's modify our class Human. Again, we have one static method averageWorking and one instance method work.

class Human {
    String name;
    int age;

    public static void averageWorking() {
        System.out.println("An average human works 40 hours per week.");
    }

    public void work() {
        System.out.println(this.name + " loves working!");
    }

    public void workTogetherWith(Human other) {
        System.out.println(this.name + " loves working with " + other.name + '!');
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

The keyword this represents a particular instance of the class.

It's easier to understand by an example:

public static void main(String[] args) {
        
    Human.averageWorking(); // "An average human works 40 hours per week."

    Human peter =  new Human();
    peter.name = "Peter";
    peter.work(); // "Peter loves working!"

        
    Human alice =  new Human();
    alice.name = "Alice";
    alice.work(); // "Alice loves working!"

    peter.workTogetherWith(alice); // "Peter loves working with Alice"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Look, now we have different outputs for the method work because two different objects have different values for name. First, we created peter and gave him a name, then by invoking peter.work() we actually saw his name in the output. We did the same with alice and also saw her name in the output.

Look at workTogetherWith method. The keyword this allows us to access a field of the particular object and distinguish it from the same field of another object.

提示

This keyword this is optional, so the code will work the same without it. But it's a good practice to add it to avoid confusion.

Of course, instance methods can take arguments and return values just as you saw in the previous topics. Returning values can be of any type including the same type as the defined class.

# Summary

In Java, every method should be declared within a class. The difference between the instance and the static methods lies in whether they interact with an object or not. Let's recap:

  • static method is associated with the class as a whole;
  • instance method can only be invoked through an instance of a class, so that you have to create an object first;
  • instance methods can access the fields of the class with this keyword.

Instance methods allow programmers to manipulate particular objects of a class. And because of it, they give us more functionality and are used more often than static methods!

编辑 (opens new window)
#Java#OOP
上次更新: 2022/09/26, 16:55:15
Theory:Defining classes
Thoery:Constructor

← Theory:Defining classes Thoery:Constructor→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式