Thursday, September 3, 2015

Constructors in java!

What are Constructors ?
They get called automatically when u create an object of a class , that's the simplest definitions we can come out with.
They have to be same name as its class.
like the following example:

public class fun { // creating the fun class

private String courseName; // set variables private



public fun (String name){ // creating a constructor

courseName = name ;


}


public String getCourseName(){ // we need this method to return the private variable

return courseName;

} // close the method

public void displayMessage(){ // displaying method

System.out.printf("this is the name %s", getCourseName());

} // close displaying method

} // close class
 
 
And then displaying and using it in main function as we explained previously.
public class Apples {
public static void main(String[] args){
fun object = new fun("Java class");
 

object.displayMessage();
 
}
}
// Thank you :D
we are going to talk next about GUI and Graphics
(Using Dialog Boxes)

 

1 comment:

  1. I thought I commented on this class but I must not have . This was a good class and I am enjoying it . Thank you for your help and making it a little easier to understand it better .

    ReplyDelete