- BASICS
- Classes
-
Objects
- Arrays
- Variables
- Loops
- Numbers
- Strings
- Exceptions
- Regexp
- OOP
- Inheritance
- Polymorphism
- Static Keyword
- Abstract Keyword
- Interfaces
- Constructors
- Packages
- Nested Classes
- Final Keyword
- SWING
- Frame
- Panel
- Listener
- Combo Box
- Label
- Image
- Menu
- Table
- Layout
- Drawing
- Timer
- Designer
- COLLECTIONS
- Lists
- Comparable
- Sets
- Maps
- Generics
- Properties
- Streams
- Json
- COMPILER
- Sublime Text
- Apache Ant
- I/O
- Streams IO
- Socket
- Watching Files
- Logger
- Clipboard
- Encrypt
- JAVAFX
- Openjfx
- Scene Builder
- First App
- Jar Archive
- On Action
- Change Listener
Objects
/**
* To use an object you must declare a reference variable
* JVM allocates space for the object
*
* The reference variable is forever of that type
* myDog = new Cat(); // will throw a type mismatch error
*/
package com.minte9.basics.objects;
public class Objects {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.size = 40;
myDog.bark();
}
}
class Dog {
int size;
void bark() {
System.out.println("Ham Ham"); // Ham Ham
}
}
References
/**
* A reference to an object can be overridden
*
* After a overrites b, the b reference to object 2 is destroyed
* Eligible for Garbage Collection
*/
package com.minte9.basics.objects;
public class References {
public static void main(String[] args) {
Book a = new Book("A");
Book b = new Book("B");
System.out.printf("%s%s /", a, b); // AB
Book c = b; // refc, objB
System.out.printf("%s%s%s /", a, b, c); // ABB
b = a; // refb, objA, objB destroyed
System.out.printf("%s%s%s /", a, b, c); // AAB
}
}
class Book {
String name;
public Book(String name) { // contructor
this.name = name;
}
public String toString() {
return name;
}
}
Last update: 219 days ago