Java 101 - Oriented Object Programming
Object Oriented Programming vs Procedural Programming?
Object oriented code can be quickly changed and since it's based on several classes one does not have to re-write the whole program but rather just change classes.
There are two kind of classes on Java.
Special Classes: They pass to the JVM in order to start and require a main method.
Regular Classes: Do not need a main method.
How can we run a class without a main method. We can instantiate it as an object in our code and then read/write to the instance variables and call the methods from other objects.
This is our main method:
public class ExternalClass
{
public String name;
public int age; public void myMethod()
{
String status = "love"
System.out.println("My name is " + name +
"and I am" + age + "year's old");
System.out.println("and I + status + "you");
}
}
A quick note about scope. Variables have scope. Scope is the portion of the code that a variable exists within. Meaning the String “love” called status cannot be accessed outside of the method.
Now let's build our main class that will instantiate our ExternalClass.
public class MyClass
{
public static void main ( String args [] )
{
//Instantiate ExternalClass
ExternalClass myObj = new ExternalClass();
//Access variables of ExternalClass
myObj.name = "Gonzalo"
myObj.age = 25;
//Run the method
myObj.myMethod();
// Would output:
// My name is Gonzalo and I am 25 year's old
}
}
Congratulations!!!
You just wrote your first object oriented program.