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 ro

Constructor: 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 CarTest

You 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 paramters. 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 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.