Java Class

 

Everything in Java is either an a primitive like int, double, etc or an object. An object is created from a class.

A class is like an object blueprint, an object constructor, or object template from which objects are created. It defines the behavior and attributes of the object.

After it is set up you can then create objects from it. You can think of it as a car. A car has attributes such as style and color and functions or methods such as drive and stop.

To create a class:
1. Use the keyword class
2. Use an uppercase class name

class User{

  String name;

  public void sayHi(){
    System.out.println(name);
  }

}

To create an Object

public class Main{

   public static void main(String[] args) {
    User user = new User();
    user.name = "Folau";

    System.out.println(user.name);// Folau

    // call method
    user.sayHi();// Folau
  }
}

 

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *