Objects
Not just objects, but OOP.

package myworld;
// Car - simple example only!
// Author: Safa
public class Car {
// properties
String brand;
int year;
boolean isRunning;
// constructors
// methods
public void run() {
isRunning=true;
}
public void roar() {
System.out.println( brand + " car is roConstructor: Creates connection between the given arguments and class variables/
The policy
package myworld;
// Car - simple example only!
// Author: Safa
public class Car {
// properties
String brand;
int year;
boolean isRunning;
// constructors
// methods
public void run() {
isRunning=true;
}
public void roar() {
package myworld;
// Car - simple example only!
// Author: Safa
public class Car {
// properties
String brand;
int year;
boolean isRunning;
// constructors
public Car( String theBrand, int theYear) {
brand = theBrand;
year = theYear;
isRunning = false;
}
// methods
public void run() {
isRunning = true;
}
public void roar() {
if(isRunning){
System.out.println( brand + " car is roaring" );
}else{
System.out.println(brand+ " car is not
running");
}
}
}
package myworld;
// CarTest - demo Car class
// Author: Safa
public class CarTest {
public static void main( String[] args) {
// VARIABLES
Car myCar;
Car yourCar;
// PROGRAM CODE
myCar = new Car( "Toyota", 2000);
yourCar = new Car( "Subaru", 2023);
myCar.run();
myCar.roar();
myCar=yourCar;
System.out.println("After the cars are equal");
myCar.roar();
yourCar.roar();
}
} // end of class CarTest
with the new stuff, it creates objects, scanner, random, etc. It creates objects, hence why you needed to import stuff outside of the class.
With declaration/initilizing, first one sets it up, while second one creates a actual object.
With the last one, you need to call the object to the method, not the general class.

If you create two objects, and assign one to the other, the lost pointer one is gone, and it gets deleted, can’t be referenced again.
With the example, since you moved the car variables pointing to, it will call only one, over and over again.

Thing to note: Like with subclasses, you can use one object into other classes, with the whole parent-child thing, but i think also with references.
package myworld;
// Car - simple example only!
// Author: Safa
public class Car {
// properties
String brand;
int year;
boolean isRunning;
// different constructors
public Car( String theBrand, int theYear) {
brand = theBrand;
year = theYear;
isRunning = false;
}
public Car() {
brand = "Toyota";
year = 1980;
isRunning = false;
}
public Car(boolean runState) {
brand = "Toyota";
year = 1980;
isRunning = runState;
}
}
package myworld;
// CarTest - demo Car class
// Author: Safa
public class CarTest {
public static void main( String[] args) {
// VARIABLES
Car myCar;
Car yourCar;
// PROGRAM CODE
myCar = new Car( "Toyota", 2000);
yourCar = new Car();
Car anotherone= new Car(true);
System.out.print(yourCar.year); // 1980
}
} // end of class CarTestYou can use the same variables to construct multiple objects, they just need to be different parameters, and when creating them, it will know which one you will need, based on the parameters.
With reading/writing, you can variables and methods with object.(variable). (like C)
Objects 2

Why typically not write main in the “main” class?
Since the libraries are silent, and it’s better not to have two main methods, don’t know which to call. Better to have the tester class outside of the “Car”class, and main should be in the tester class. (if you remember from before, you typiclally did this, one with methods, another with running the methods)
All classes should be public, and one class per file, not multiuple stacked together.

From the beginning, even the classes can be treated as objects, and even the constructor was predefined.
Wrong way to make objects

JAva doesn’t use () for creating objects, and for the second one, never created an object, you need “new” to create a new object.
that said, the second one is a declaration, you can initialize it via next line, and it will work, with new added.


With int, 0, with string, NULL.
With instance variables of objects, better to set them inside of the constructor methods, like always, rather than doing it in main/other function. (you would call the method to init it)
Public vs Private
With public, anyone inside the package can access, but with private, it’s more secluded, not being able to access it, except the class itself, nowhere else for the other classes, even if they are in the same package.
With two mains in two classes, you have to specify which one to run first.
IF main is in reg class, it will run.
Okay, I see, with these classes, you want them to naturally run them as objects, and just manipulating the free “object” that comes with it.

Default is when you don’t have the other 3 modifers, and it acts the same as protected.
For instance variables(Variables outside of main) use private, so that it’s local to the class only, and can’t be modified.

From back then, very useful for objects.
Used for reading and writing/altering variables.
USe this to change variables, instead of changing directly, since they would have private with them anyways.

To protect from PDF reader not being writing/changing?
Only have getYear, or have setYear private.
Why getters and setters are useful?

You can use it to flag for invalid years, in this case, or just for invalid results in general.
Why the validity checks?
For complex equations being set to year, or any var, better to have the check.
For getters, need to return, can’t accept anything as parameters.
For setters, while it might be “practical” for multiple values, if only needing to change one, doesn’t make sense, so just use it to change one each, so one parameter.

getters/setters can be used in the main class, not just for tester class, and they can be resitcted to just the main class only.
If you set up final private, default is 0, so can’t be changed.
If you want to change it, initiaize it only in the declaration, not in the constructor.

Don’t initalize it inside the instance variable, use it inside the constructor.
Reason why, since it is better to use the constructor, so you can see it.
Why use “this” keyword, since it is a varaible for holding the same name as a variable.
IF your setter uses the same name as the variable in the object, use
this.year= year;Waaay better to use a different parameter name, instead of using this.(sign of AI code using this for no reason, very reduntant)

With this, it passes the value by copying the original reference.
For like the fixing
fixYear(Car c){
c.year=0;
}that is what it means, it can change objects, since it gets the copy of the reference, and is manipulating that reference.
With changeStr, doesn’t work, since no access point.
With fixBrand, since it has a memory locaiton, it can change the brrand name.
with
NullCar(Car yourcar){
yourcar = null;
}// not possible, since you are messing with memory, and in java, you can't do that.
// moreover why, it crfeates a new arm/pointer to change it, but the real reference is safe, it nulls the new pointer instead. Object Methods
toString: convert to string(better to make it yourself for this, and
equals: checking if two objects are equal, and equals)
hashCode
clone
etc
With making your own toString format, don’t make it void or add parameters. It will crash it, since you are messing with the original toString, i think.
When calling toString
System.out.println(mycar.toString())// reference one
System.out.println(mycar)// second way, since it will grab it normally
// toString should return a string, rather than printing something. For creating object methods
The creation methods
Also, make sure to add toString, always needed for those types of problems.
// with Arraylist
garage.add(new Car(188)); // this is valid as well!Objects 3

It is copying the car, by getting the original variables, and setting them to another car object, the copy.
This is a Deep Copy, since it creates a whole new object.


Essentially, with a static method from another class, you can access it with the object since it’s created from the same class, but calling it by itself, it doesn’t work, since it is not from the tester class.
Best for classes you want to write for objects, not for instance methods.
With static/non-static, you can’t have two different methods with the same name, with one static and one non-static.
Also, just reinforcing between static and non-static, static methods can’t access getters, setters, car methods, etc. Mainly try to return something, like a simple check, rather than a method that calls other methods, etc.

With this, there is a private static instance variable. With this, you can access count in a non-static instance method; also, you are sharing the same variable.
With static instancfe variables , useful for creating an order number for each object, reference to an object, and for validation check for static methods.
with calling something, referencing the array element, two ways it doesn’t work is when you call something that doesn’t exist(like calling 7 when there are 5 elements) or nothing at all. same error, probably arrayIndexOutofBounds.

Same thing discussed before, make sure to have different paramteres for methods for the same name.
Also, if they have same parameters, but different data types, but also same name, it won’t know, known as ambiguous methods.

return this; // returns the current instance of the object itself
// would mean w.getSelf, would return w itself.
// use case, would be getting transcations from itself, ot grabbing from its account. Constructor Call and this.
The constructor with the same name as the class can’t be called inside the Main Class/Object class.
this can only be called inside of constructor/referrencing.
wriitng this. before any method/instance variables, like
this.getTurn();is syntacitcally correct, but ethically not. Doesn’t make sense to highlight itself twice. Don’t write this.(method) unless there is a confusion.
Cyclic calls between constructors will be detected by the complier.
After OOP Objects
Primitative varibales are still primitive, while non-primitative ones are objects, hence why use equals to check equality between them.
With strings, it hold char array and length, and toString method.
Getting size with
.length()Can be defined with String name = new String (“test”);// though the memory is different compared to regularly calling strings.
Arrays are also objects
int[] arr=new int[num];with length here, it is a public final variable in array object, not a method, hence why only use it length, not length().
// NO PARENTHESES
Arraylists are also objects, which holds arrya with variable length, extending the array as needed, but takes some generic to be put inside, extends but doesn’t shrink.
Random and Scanner are both objects, with Random somethingrandom = new Random();
and
Scanner anotherScanner=new Scanner();Math is an object, but all of their methods are static.
Math is a private constructor, hence why you can’t create an instance of Math class, hence why you always just call it, like Math.sin(), while Pi is a final instance variable in Math class.
Main is static for two reasons
In order to be run, it would have to be an object if it doesn’t have static. It is inside of a class object, so how would it run?
Main is the main thing running everything, not related to any class, so it should be static.
Can you use Main inside Main?
Could, but it would crash the JVM, since it wouldn’t know which one would run, and causing a memory leak, since it would skip the previous main.
From last time,
pass by value by sending a copied reference, so can change the orignial box it is pointing to, but can’t null it.
Can you null int?
Yes, if you do
Public int val();// and in main
// set it to NULL, can set the value to NULL, but not the refernce itself. Classes are all objects, hence why object/main class exists. Can call by the name of the class.
How java.util.Scanner works.
Java is a package, and util is another package, and Scanner exists therejava.lang.*;// default, has string, System.outDon’t use static import!!
Copying all the methods at once, so basically using all methods in the class, so you can get
sqrt(16); // without math classPrintln()
is translated to
System.out.println(someObject.toString());that’s why you can print a StringBuilder without toString
System.out.println(new StringBuilder(“Java”));