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]:
- There can only one public class per source code file.
- If there is a public class in a file, the name of the file must match with the name of the public class.
- 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.
- If there are import statement, they must written between the package statement and the class declaration.
- 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);
}
}