Defining a Class and Methods in Java

Java is known as a pure oop language. that means every thing is defined in the form of class and used as object. Lets now see the structure of java class and objects.

Definition of Class

Java Classes are the blueprint of what you want to do with the program. here we are going to define the class and let you know about the code structure and core construct of a java class.
I am again going to take the example of the car.

//defining the class car
Class Car{
    //defining properties
    private int gear;
    private int speed;
    private boolean transmissionMode;
    //defining the behaviour
    public int getSpeed(){
        return this.speed;
    public void setSpeed(int s){
        this.speed = s;

Now let's look into this class and its syntax to see what we have done in the little program above.
first of all the syntax of the class definition. Its using the keyword "class" followed by the name of the class. If you have noticed the first letter of the car capitalized, well don't worry about this. This is not the essential syntax but is just a common practice used by Java programmers. the class names are in this way capitalized, if there are more than one word in the name the first letter of the each word is capitalized.

Anyway, as I have already mentioned in the introduction of the class that it contains the properties and behaviour for how those properties would work. That is what is been done inside the body of class, i.e. the properties(variables) and behaviours(have been defined). when you will initialise this class, each of its object will contain a unique copy of these properties. In our example we have defined three properties for the car. Gear, speed and transmissionMode. and to manage these properties there are two methods, getSpeed and getSpeed there will control the behaviour of the speed for our class.

Definition of Methods

We wont go for defining another class but I will explain using the same method what are the methods. As you see in the above example there are two methods are defined. getSpeed and setSpeed. the syntax of defining the method in Java is as follows.

Scope-Identifier Return-Type methodName(Argument-Type arguments){
//body of the method
//return statement if required

Now the scope identifier will tell you the access limit of the method, there are four types. private(limited access allowed), default(accessible within same package), protected(limited access in other package) and public(accessible everywhere). After specifying the scope limit.

Return-Type is the type of value which is return by a method after completing the execution of its body. It may be defined as void if there is no return value.

methodName is of course the unique identifiable name of that method. this is usually in camel case for example "thisIsCamelCase". Method name is always specified with the '()' with the parameters inside if you want to pass any value to the method. for example for a method which will sum two numbers and return result you can define as

public int sum(int a, int b){
    return a+b;