Java: Berbagai Istilah

From OnnoWiki
Revision as of 17:54, 6 September 2011 by Onnowpurbo (talk | contribs)
Jump to navigation Jump to search

Tulisan ini menjelaskan berbagai istilah yang sering digunakan di Java


Definisi

Sangat penting bagi kita untuk mengerti struktur dasar dari package, class dan object.

Dasar: Package, Class dan Object

Package

Java mengelompokan class menjadi package-package fungsional. Semua class di java.lang akan secara automatis tersedia di java program.


The main reason of packages is to avoid naming collisions. One programmer may create a java class Test. Another program may create another class with the same name. With the usage of packages you can tell the system which class to call. For example if programmer 1 put the class test into package programmer1 and the second programmer puts his class into package "xmlreader" and you would like to access the second class you can do this via xmlreader.Test.

The other important reason is that via packages you can group your classes into logical units. 1.1.2. Class

Def.: Template that describes the data and behavior associated with an instance of that class.

The data associated with a class is stored in variables; the behavior associated to a class or object is implemented with methods.

Can be seen as the blueprint of object.

A class

   Is defined by the keyword class
   Start with a capital letter
   Body of a class is surrounded by {}

1.1.3. Object

Def.: An object is an instance of a class.

While a class can be considered as a blueprint for the data and the behavior of an object the object is the real element which has data and can perform actions.

For example as class may be the blueprint of a car, an object is then a real car.

Every object extends implicitly the Object “Object” and get wherefore gets the methods defined by class Object

   o.equals(o1) Is the Object equal to o1
   o.getClass() Returns the class of o
   o.hashCode() Returns a unique ID o.toString()

1.2. Variables 1.2.1. Variable

Variable can either be a primitive or a reference variable. A primitive variable contains value while the reference variable contains a reference (pointer) to the object. Hence if you compare two reference variables then you compare if both point to the same object. To compare objects use object1.equals(object2). 1.2.2. Instance variable

Instance variable is associated with an instance of the class (also called object). Access works over these objects.

Instance variables can have any access control and can be marked final or transisient. Instance variables marked as final can not be changed after assigned to a value. 1.2.3. Local Variable

Local (stack) variable declarations cannot have access modifiers.

final is the only modifier available to local variables

Local variables don't get default values, so they must be initialized before use. 1.3. Modifiers 1.3.1. Access Modifiers

There are three access modifiers: public, protected and private

There are four access levels: public, protected, default and private

protected and default are similar. Only difference: A protected class can get accessed from the package and sub-classes outside the package while a default class can get only access via the same package protected = package plus kids 1.3.2. Other modifiers

final methods: cannot be overwritten in a subclass

abstractmethod: no method body

synchronized method: threat safe, can be final and have any access control

native methods: platform dependent code, apply only to methods

stricttp: class or method 1.4. Interface

Within java an interface is a type just as a class is a type. Like a class an interface defines methods.

Interfaces are contracts for what a class can do but they say nothing about the way in which the class must do it.

Interfaces are per default public and abstract – explicit declaration of these modifiers is optional.

An interface can also have abstract methods, no concrete methods are allowed.

Interfaces can have constants which are always implicitly public, static and final. 1.5. Additional Definitions 1.5.1. Method

A method can trigger a certain action.

Method with var-args: Method declares a parameter which accepts from zero to many arguments (syntax: type .. name;) A method can only have one var-args parameter and this must be last parameter in the method.

Overwrite of a superclass method: A method must be of the exact same return parameter and the same arguments. Also the return parameter must be the same. Overload methods: An overloaded method is a method with the same name, but different arguments. The return type can not be used to overload a method. 1.5.2. Constructor

classname (Parameter p1, ..) {}

The constructor is always called if the class is created. If no explicit constructor is defined the compiler adds implicitly a constructor. If the class is sub-classed then the constructor of the super class is always implicitly called. 1.5.3. Class method or class variable

Class method / variable is associated with the class and not the instance. To refer to it use the classname and the class method / variable together with a period ("."). Example System.out.println("Hello World"). The runtime environment associates one class variable for a class no matter how many instances exists.

Class method / variant does exists once for all instances. They are indicated with the word static. If a variable should be existing for all instances and be non-changeable (=CONSTANT) then also the word final is used.

The static method runs without any instance of the class. Does not depend on instance (non-static) variables. Can not use the this object or an instance variables.

The static variable is the same of all instances of the class (global variable) 1.5.4. Abstract class

If a class has one method which only contain the declaration of the method but not the implementation then this class is abstract and can not be initialized. Sub-classes need then to define the methods except if they also declared as abstract. method abstract double returnDouble();

If a class contains an abstract method it also needs to get defined as abstract.


Referensi

Pranala Menarik