Class Declaration

12 0 0
                                    

class - a class is a blueprint of the real object you wanted to create. It contains fields (instance and static variables), constructors (where the object is initialized), methods (this is the behavior or the function of the class usually based on fields or properties). It can only extend one parent class (this is the super class where its properties and methods can be use by the child class) but can implement more than one interface (this is more like an abstract class where it contains abstract fields and abstract methods that require an implementation).

Different kinds of variables

1. member variables - also known as fields

2. local variables - those variables declared within the block or body of the method.

3. parameters - those variables declared within the parenthesis of the method during method declaration.

Modifiers - they are used to indicate the accessibility of class, fields or methods and etc.

1. public - the fields and methods are accessible from all classes.

2. private - the fields and methods are only accessible within its own class. It is common to make fields private and should access only through set and get public methods.

=============================

class Student{

    // below is a field

    private String name = "John Doe";

    // below are the methods (the set and get)

    public void setName(String yourName){

        name = yourName;

    }

    public void setName(String first, String last){

        name = first + " " + last;    

    }

    public String getName(){

        return name;

    }

}

===========================

Take note of the use of comments in here:

// - this double forward slash means a line comment. Whatever words you put after the two slash will be treated as a comment and not a code as long as it within the line of the two forward slash.

/* yours words */ - this is a multi line comment. This forward slash followed by asterisk with words in the center and ended with asterisk followed by another forward slash is a multi line comment. Meaning, you can extend your words to another line as long as it is still in between of /* and */ symbols.

/** your words */ - this a documentation comment. This is the same with multi line comment but the usage is for documentation. A documentation is where you explain why you are doing such programs. This is where the other programmers usually take their hints on how the program works.

Method contains a modifier, return type or void if none, name of the method in camel case, a parenthesis which sometimes has a parameter and a block or body where you put your statements to manipulate the variables. Note that when you call a method with arguments, make sure that the type and order of parameters does match with the arguments. Java supports method overloading.

Method Overloading - these are methods that contains the same name but varies in parameters just like the methods above named setName(String yourName) and setName(String first, String last). Though, they don't advise us to use method overloading since it can get complicated.

Java ProgrammingWhere stories live. Discover now