classes in java
Java
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();
}
}
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
// 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());
}
}
Also in Java:
- Title
- java hash password
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- python vs java
- Category
- Java
- Title
- how to get all of the chldren of a panel in java
- Category
- Java
- Title
- fastapi list query parameter
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- java making a screen
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- how do you make a method that returns an array java
- Category
- Java
- Title
- java find duplicates in array
- Category
- Java
- Title
- leap year program in java
- Category
- Java
- Title
- android foreground push notification
- Category
- Java
- Title
- java 8 findany on null list
- Category
- Java
- Title
- java new string with values
- Category
- Java
- Title
- get certain character from string java
- Category
- Java
- Title
- jtable fill panel
- Category
- Java
- Title
- java sum of two numbers program intellij
- Category
- Java
- Title
- how to add an image to a gui in java windowbuilder
- Category
- Java
- Title
- how to change maven java version in po,
- Category
- Java
- Title
- bootstrap center text vertically
- Category
- Java
- Title
- java how to stop code
- Category
- Java
- Title
- java console text color
- Category
- Java
- Title
- java get current date without time
- Category
- Java
- Title
- how to push an element in hashset java
- Category
- Java
- Title
- convert from integer to character java
- Category
- Java
- Title
- java how to define a function
- Category
- Java
- Title
- list in java
- Category
- Java
- Title
- string a int java
- Category
- Java
- Title
- BoxLayout java
- Category
- Java
- Title
- spigot respawn player location
- Category
- Java
- Title
- how to create a Scanner Object in java
- Category
- Java
- Title
- how to make stage dragable in javafx
- Category
- Java
- Title
- alfred workflow
- Category
- Java
- Title
- arraylist remove method java
- Category
- Java
- Title
- java convert a set to array
- Category
- Java
- Title
- java for in loop
- Category
- Java
- Title
- android notification addaction example
- Category
- Java
- Title
- load contents of file into string java
- Category
- Java
- Title
- java while loop example
- Category
- Java
- Title
- loop through array java
- Category
- Java
- Title
- java string split underscore
- Category
- Java
- Title
- how to convert char to uppercase java
- Category
- Java
- Title
- Spigot API inventory close
- Category
- Java
- Title
- how to make javafx full screen default
- Category
- Java
- Title
- java int to hex fixed length
- Category
- Java
- Title
- java while loop break
- Category
- Java
- Title
- how to play an audio in java
- Category
- Java
- Title
- how to add element to arraylist
- Category
- Java
- Title
- string replace last character java
- Category
- Java
- Title
- java split string on two or more spaces except for words in quotes
- Category
- Java
- Title
- iterate map in java 8 using stream
- Category
- Java
- Title
- google map set default zoom android studio
- Category
- Java
- Title
- java instantiate collection with values
- Category
- Java
- Title
- login and logout react native and firebase
- Category
- Java
- Title
- find length of array java
- Category
- Java
- Title
- boolean checkbox swing
- Category
- Java
- Title
- how to quicksort a string array in java
- Category
- Java
- Title
- hello world in java
- Category
- Java
- Title
- how to remove a certain string in a arraylist java
- Category
- Java
- Title
- java builder pattern example
- Category
- Java
- Title
- how to write a perfect shuffle method in java
- Category
- Java
- Title
- Error executing Maven. java.io.FileNotFoundException: The specified user settings file does not exist: /etc/java-8-openjdk
- Category
- Java
- Title
- get tfidf score for a sentence
- Category
- Java
- Title
- intellij replace all
- Category
- Java
- Title
- javafx polygon
- Category
- Java
- Title
- substring java
- Category
- Java
- Title
- reverse a string in java
- Category
- Java
- Title
- java vector push_back
- Category
- Java
- Title
- java store hexadecimal value
- Category
- Java
- Title
- java interface
- Category
- Java
- Title
- how to count an replace string in java
- Category
- Java
- Title
- indexof java
- Category
- Java
- Title
- .length array java
- Category
- Java
- Title
- remove unused imports intellij
- Category
- Java
- Title
- creating random color in java
- Category
- Java
- Title
- java hashmap set value
- Category
- Java
- Title
- how to make generic arraylist in java
- Category
- Java
- Title
- java int passed by reference
- Category
- Java
- Title
- rgb to hex java
- Category
- Java
- Title
- cgange background from button click java fx
- Category
- Java
- Title
- minecraft sounds spigot
- Category
- Java
- Title
- taking date as input in java
- Category
- Java
- Title
- transformer un string en double java
- Category
- Java
- Title
- log4j print stacktrace
- Category
- Java
- Title
- java define a generic class that produces
- Category
- Java
- Title
- declare array java inline
- Category
- Java
- Title
- Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification
- Category
- Java
- Title
- validate isbn number java
- Category
- Java
- Title
- spring-boot actuator not working
- Category
- Java
- Title
- java switch
- Category
- Java
- Title
- java comments
- Category
- Java
- Title
- how to pass a float between activities in android studio
- Category
- Java
- Title
- Fragment DashboardFragment not associated with a fragment manager.
- Category
- Java
- Title
- import
- Category
- Java
- Title
- swapping of two numbers in java
- Category
- Java
- Title
- java get excectuon time
- Category
- Java
- Title
- how to create a draw Rectangle in java
- Category
- Java
- Title
- how to create array of linkedlist in java
- Category
- Java
- Title
- take a peice from array java
- Category
- Java
- Title
- ternary operator java
- Category
- Java