Class Java

The basic construct of programming is Class Java like the basic constructs for a building is sand, cement and steel. Inside the class, exists other constructs like main, methods, variables and constructors etc.

Java demands to write any code of any thousands of lines, within the class declaration only – between the open brace, {, and close brace, }. Outside the braces not allowed.

Observe the following specimen code.

public class Employee                     // class Java declaration starts
{                                         // open brace of the class
  String name = "S N Rao";                            // two variables
  double salary = 9999.99;

  Employee()                              // constructor
  {
     System.out.println("From Constructor Name: " + name + " Salary: " + salary);
  }
                                          // methods
  void calculate()                        // first method
  {
     System.out.println("From calculate() method Name: " + name + " Salary: " + salary);
  }
  void show()                             // second method
  {
     System.out.println("From show() method Name: " + name + " Salary: " + salary);
  }
                                          // at the last, main() method
  public static void main(String args[])
  {
    Employee emp1 = new Employee();       // create an object of Employee
    emp1.calculate();                     // with object call calculate() method
    emp1.show();                          // with object call calculate() method
  }                                       // close brace of the class Java
)


Class Java
Output screenshot on Class Java

When the programmer gives the execution command, the public class is loaded into the RAM. The execution starts from main() method. For C/C++ people, one strange thing is to compile a Java program, main() is not necessary. That is, to compile Java program, main is not required but to execute main is required.

Know more about classes in Java with the following links of this same Web site, way2java.com.

Following tutorial gives easy entrance to Java.

1. Java Features – Buzz Words
2. Primitive Data Types
3. Data Types Default Values – No Garbage
4. OOPS concepts – introduction
5. Java Naming Conventions – Readability
6. Basic Class Structure, Compilation and Execution
7. Using Local and Instance Variables
8. Three Great Principles – Data Binding, Data Hiding, Encapsulation
9. Using Variables from Methods

More elaborate discussion of classes, abstract classes and interfaces is given in "Java Classes and Interfaces".

Even though, the above topics give you an easy approach to learn, I advice you to take help of your friend knowing Java; the learning goes faster (even you have good programming skills in other languages).

Leave a Comment

Your email address will not be published.