Posts Tagged ‘class’

Java Tips: Class

Monday, May 18th, 2009

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share.

A class can represents a person, place, or thing as an abstraction of a concept within a computer program. Fundamentally, it encapsulates the state and behavior of that which it conceptually represents. It encapsulates state through data placeholders called member variables (or instance variables). It encapsulates behavior through reusable code called methods.

There is some rules associated with declaring classes in a source file [SCJP 1.6 Study Guide]:

  1. There can only one public class per source code file.
  2. If there is a public class in a file, the name of the file must match with the name of the public class.
  3. If the class is part of the package, the package statements must be the first line inte source code file before import statement that maybe present.
  4. If there are import statement, they must written between the package statement and the class declaration.
  5. A source code file can have more than one nonpublic class.

The following code is the bare-bones class declaration:

class MyFirstClass() {
  //.......
}

Ok, lets start to make a hello world class :)

public class MyHelloWorld() {
  private String greet = "Ehlo World";
  public void says() {
    System.out.println(greet);
  }
}

Java Tips: Identifiers

Friday, April 10th, 2009

On java programming language, the name of classes, variables, methods called an identifier. Java has some rules about identifiers so that identifiers are legal for use.

Technically, legal identifiers must be composed of only Unicode Characters, number, curency character, and connecting character (like underscores).

Then, here are the rules you do need to know:

  1. Identifiers must start with letter, a curency character ($), or a connecting character. It is can’t start with a number.
  2. After the first character above, identifiers can contain any combination of  Unicode Characters, number, curency character, and connecting character.
  3. There is no limit to the number of characters an identifiers can contain.
  4. Can’t use java keywords as an identifiers
  5. Like the java programming languange habbit, the identifiers are case-sensitive.

Happy Coding… :)