06. Methods & Classes

When you sort an array of ints do the numbers move?

Yes.

When you sort an array of Objects do the Objects move?

No.


Why create Methods?

Shorter programs

- Long methods tend to have too many responsibilities

- Long methods tend to be harder to understand and maintain

Easier to understand programs

Allow abstraction


Parameterization and Method Structure

Access modifier return type method name parameter list

public boolean loginOK (String user, String password) {

}

* Method name and parameters are parts of METHOD SIGNATURE


Return - A new statement

Notice the return statement

- Exits the method. More specifically, it returns the control to its caller

- Provide a return value

Return value must be provided if the declaration of the method is not void

Type of value returned must match the return type in the method header


Strings

You can't change Strings

- You can refer to a portion of a String

- You can copy Strings (or portions of them)

- You can make new Strings

Interest methods:

- charAt(), length(), equals(), substring()


Identity vs. Equality

"==" to check identity

- Whether two objects share the same memory address

"equals" method defines equality of two objects

- Usually checks if two instances (objects) contain the same state

- Object class has equals method which checks identity!!!

- When writing your own class, you need to think about how to define equality (in other words, need to implement equals method)

- For example, equals method of String class


Strings and ints

Converting from Strings to ints

- Integer.parseInt()

Converting from ints to Strings

- String.valueOf()

- System.out.print()


StringBuilder

In StringBuilder, contents can change

Interesting methods:

- append(), delete(), insert(), length(), replace(), reverse(), toString()


Note: Style of returning "this"

StringBuilder's append() method returns this StringBuilder instance


Class

Define the class in a file with the same name

- Specify the superclass (or just default for Object)

Specify the variables

- Instance variables (a.k.a. member variables)

- Class variables (a.k.a. static variables)

Specify the methods

- Constructors

- Instance methods (a.k.a. member methods)

- Class methods (a.k.a. static methods)

When declaring variables and methods

- Use static to make it a class variable or class method


Instance method:

Invoke instance methods using a reference to an instance

Class methods:

Invoke class methods using the name of the class

e.g. Integer.parseInt(s)

Primitive wrapper classes:

- Character, Integer, Byte, Short, Long, Float, Double, Boolean (all start with capital letters)

- The way to store primitive values as objects

e.g. "new Integer(s).intValue()" the same as Integer.parseInt(s)

- A collection of methods to manipulate primitive values


Static Variables and Methods

Static variables are shared by all instances of the class!

- Use static variables only if you want all the instances of a class to share it

- If one instance changes the value of a static variable, all instances of the same class are affected

Static methods CANNOT access instance members of the class

You can access public static variables and static methods from both an instance reference and a class name

- But, please USE a class name to improve readability and avoid errors


Common Errors

Define instance variable(s) or method(s) that should have been static

Define static variable(s) or method(s) that should have been instance


Instance variables are unique to each instance of the class

Static variables belong to the class, Not to any individual instance of the class

Static variables are accessible from constructors, instance methods, and static methods


Getters & Setters (Encapsulation)

a.k.a Accessors and Mutators

Field are private

The getter methods provide read access

The setters provide write access

- No setter method =>makes something read-only

- Immutable objects have no setter methods

Strings are immutable

Dates should be immutable


Overloading vs. Overriding

"Overloading" is when you have many methods with the same name, but different parameter lists which means different signatures

E.g. System.out.println(...)

StringBuilder's append(...)

"Overriding" is when you replace a superclass method in a subclass (same signatures)

E.g. toString()

Note: method signature consists of its name and parameter lists. The method's return type is not a part of the signature


Java Package

A package is a collection of related classes

- Main reason is to guarantee uniqueness of class name

We've been using the java.lang package

- Automatically or implicitly imported already

- Do not need to write import for any class in java.lang package

There are many others

- E.g. java.util and java.io


To use a class from a package, you must either:

- Specify the package when using the class (tedious)

- Import the class using an import statement

- Import * (wildcard import)

* bytecodes in class files are the same (using full name) because import statement simply tells the complier where to locate the class


Visibility (Access) Modifiers

Use public to make it accessible outside the class

Use private to make it accessible only from within the class

Use protected to make it accessible from subclasses


Compare & Constrast

An instance method

- Can invoke an instance method

- Can access an instance variable

- Can invoke a static method

- Can access static variable

A static method

- Cannot invoke instance method

- Cannot access instance variable

- Can invoke a static method

- Can access a static variable

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值