Computers/Language java

week3. Implementing Classes (Basic Java Concept part2)

emzei 2011. 4. 10. 14:41

 Instance Variables 

Instance variables store the data of an object

Instance of a class : an object of the class

- The class declaration specifies the instance variables :

public class Counter

{

   private int value;

   ...

}

- An instance variable declaration consists of the following parts :

access specifier ( private )

type of variable ( such as int )

name of variable ( such as value )

- Each object of a class has its own set of instance variable

- You should declare all instance variables as private



◆ Accessing Instance Variable

* Private instance variables can only be accessed by methods of the same class



◆ Instance Variable

 Encapsulation : the process of hiding object data and providing methods for data access

* To encapsulation data, declare instance variables as private and declare public methods that access the variables

* Encapsulation allows a programmer to use a class without having to know its implementation

* Information hiding makes it simpler for the implementor of a class to declare to locate errors and change implementations



◆ Specifying the Public Interface of a Class

 Interface : collection of abstract methods(method without body)

└▶ abstract methods ~ like method's header !!! >>> withdraw(); 같은것들

└▶ purpose : 절차를 명확히 하기 위함!!!

- Methods/ Method declaration/ Method Header/ Constructor Declaration

※ Constructor도  override 가능!


 Local variable

Local and parameter variables belong to a method

Instance variables belongs to an object, not method

- In Java, the garbage collector periodically reclaims objects when they are no longer used

- Instance variable ~ initialize to a default value

- Local variable ~ must initialize !!!



◆ Implicit Parameter

- The implicit parameter of a method is the object on which the method is invoked

 ex.  momsSavings.deposit(500);

└>implicit parameter  └> explicit parameter


◆ Implicit Parameter and this

- current active object

- The this reference denotes the implicit parameter


  ex. 

balance = balance + amount;

actually means,

this.balance = this.balance + amount;

- When you refer to an instance variable in a method, the complier automatically applies it to the this reference.



'Computers > Language java' 카테고리의 다른 글

week5. Decision  (0) 2011.04.10
week4. Fundamental Data Types  (0) 2011.04.10
week2. Using Objects (Basic Java Concept part1)  (0) 2011.04.10
week1. Introduction  (0) 2011.04.09
eclipse 단축키  (0) 2011.04.08