Q:

what is class

public class Lightsaber {
  // properties
  private boolean isOn;
  private Color color;
  
  // constructor
  public Lightsaber(Color color) {
    this.isOn = false;
    this.color = color;
  }
  
  // getters
  public Color getColor() {
    return color;
  }
  public boolean getOnStatus() {
    return isOn;
  }
  
  // setters
  public void turnOn() {
    isOn = true;
  }
  public void turnOff() {
    isOn = false;
  }
}



// Implementation in main method:
public class test {
  public static void main(String[] args) {
    Lightsaber yoda = new Lightsaber(green);
    yoda.turnOn();
  }
}
    
7
// this might help you understand how classes work
public class MathTest {

    public static void main(String[] args) {

        class MathAdd {

            int num1;
            int num2;

            public int addNumbers() {

                int addThemUp = num1 + num2;
                return addThemUp;
            }
        }

        MathAdd addition = new MathAdd(); // create a new instance of the class

        // you can access variables from the class 
        addition.num1 = 10; 
        addition.num2 = 20;

        // and use the method from the class to add them up
        System.out.println(addition.addNumbers());

    }
}
1
Class is a blueprint or template which you can create as many objects as you 
like. Object is a member or instance of a class.
Class is declared using class keyword, Object is created through
new keyword mainly. A class is a template for objects. A class defines 
object properties including a valid range of values, and a default value. 
A class also describes object behavior.
0
A class is a block of code that holds various functions. Because they
are located inside a class they are named methods but mean the samne
thing. In addition variables that are stored inside a class are named 
attributes. The point of a class is to call the class later allowing you 
to access as many functions or (methods) as you would like with the same
class name. These methods are grouped together under one class name due
to them working in association with eachother in some way.
0

New to Communities?

Join the community