I just watched a youtube tutorial called “many methods and instances”. He made a program in which you enter something and it says “your first gf was _“. But it was way too overcomplicated. First is the main class:
import java.util.Scanner;
public class MethodsInstances2 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
MethodsInstances object = new MethodsInstances();
System.out.println("Enter name of first gf here: ");
String temp = input.nextLine();
object.setName(temp);
object.saying();
}
}
Next is the class it makes an object from:
public class MethodsInstances {
private String girlName;
public void setName (String name){
girlName=name;
}
public String getName (){
return girlName;
}
public void saying(){
System.out.printf("Your first gf was %s", getName());
}
}
It seemed WAY too overcomplicated, and the title is all of the stuff i didn’t understand, considering I’m still a newbie at Java. Here’s what I typed which took 4 times faster:
import java.util.Scanner;
public class programtest {
public static void main(String args[]){
Scanner test = new Scanner(System.in);
String name;
System.out.println("Enter the name of your first girfriend: ");
name = test.next();
System.out.println("Your first girlfriend was " + name);
}
}
Can someone tell me the point of doing what the tut said to do, and what the title words mean?
Thanks a lot, Dan
The tutorial is named “many methods and instances” and from my POV shows very simply how to create a class, instantiate it and call its methods. It would have been called “get some console input and spit it back out” if it was meant to do things the easiest way possible.
The point of the video tutorial was not to create an overly complicated program but rather to demonstrate how to call methods on objects.
You have a class called MethodInstances (not a great example name, btw) which you instantiate and then call methods on it to save state (gf name) and the get it back and display it. (I would suggest refactoring the example to have it make more sense. The verbiage as it is causes confusion.)
Try this on:
You short circuited the process by avoiding creating a separate class and using a local variable instead. It works, it’s easier, but that’s not what the tutorial was trying to teach.
I suggest finding even simpler examples for starters before getting into OOP stuff or… embracing OOP and running with it.
Btw, you will not progress as a programmer by always doing things the easiest way. Easiest only seems easiest in the near term. Any less-than-trivial project with even a bit of complexity will quickly become untenable by doing it the easiest way.