java 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();
}
}
// Example : TestClass (Can be predefined or user-defined)
public class TestClass {
// properties
private int id = 111;
// constructor
public TestClass(){
super();
}
// method
public void test(){
// some code here
}
}// 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());
}
}public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
add
Also in Java:
- how to import another class in java
- java null pointer exception
- spring framework iterate
- how to add a number to an array in java
- adding an element to the end of a linked list java
- how to make a fixed size array in java
- jtable font size
- money value commas java
- java for in loop
- java get excectuon time
- elasticsearch api code call using highlevelrestclient
- set view layout params android
- firebase connecten
- java read public key from keystore
- dicom read with java
- dates in java 8
- java stack push
- android MediaStore update cache before query
- java printf tab
- binary numbers java
- how to count an replace string in java
- java swing pic
- how to append to an array in java
- java download file from url to string