1public class Lightsaber {
2 // properties
3 private boolean isOn;
4 private Color color;
5
6 // constructor
7 public Lightsaber(Color color) {
8 this.isOn = false;
9 this.color = color;
10 }
11
12 // getters
13 public Color getColor() {
14 return color;
15 }
16 public boolean getOnStatus() {
17 return isOn;
18 }
19
20 // setters
21 public void turnOn() {
22 isOn = true;
23 }
24 public void turnOff() {
25 isOn = false;
26 }
27}
28
29
30
31// Implementation in main method:
32public class test {
33 public static void main(String[] args) {
34 Lightsaber yoda = new Lightsaber(green);
35 yoda.turnOn();
36 }
37}
38
1class Puppy() {
2 String name;
3 int age;
4
5
6 Puppy(String puppy_name, int puppy_age) {
7 name = puppy_name;
8 age = puppy_age;
9 }
10
11 public static void main(String[] args) {
12 Puppy rex = new Puppy("rex",4);
13 }
14
15}
1public class Puppy {
2 public Puppy(String name) {
3 // This constructor has one parameter, name.
4 System.out.println("Passed Name is :" + name );
5 }
6
7 public static void main(String []args) {
8 // Following statement would create an object myPuppy
9 Puppy myPuppy = new Puppy( "tommy" );
10 }
11}
1
2public class HelloWorld {
3 public static void main(String[] args) {
4
5 // how to use class in java
6 class User{
7
8 int score;
9 }
10
11 User dave = new User();
12
13 dave.score = 20;
14
15 System.out.println(dave.score);
16
17
18 }
19}
20
1Objects: came from class, we can create multiple objects from a class
2 ArrayList<> list = new ArrayList<>();
3 Class refName OBJECT
4
5 each object has its own copy of instance variable
6· declared outside the blocks or methods
7Object: Instance of the class. We can store different data's to objects
8Object Creation: with new keyword.
9 ClassName obj = new ExistingConstructor;
10