Classes and Objects | Java: Classes are custom/used data types, and Objects are instances of the data type.
Syntax: Class: class_name {
// statements...
}
Oject: class_name object_name = new class_name();
Here is the Code:
- import java.io.*;
- class Main {
- public static void main (String[] args) {
- A a = new A();
- a.x = 1;
- System.out.println(a.x);
- B b = new B();
- b.y = 2;
- b.z = 3;
- System.out.println(b.y);
- a.c = b;
- System.out.println(a.c.z);
- }
- }
- class A {
- int x;
- B c;
- }
- class B {
- int y;
- int z;
- }
Output:
1
2
3
Here is the Video: