I have started teaching myself Java but I still have this nagging issue where I am not 100% understanding these concepts. I have read multiple threads, subs, and user explanations but am still so slow at picking up this concept. I think I understand the following:
A class is a blueprint
A method is a function(action) within a class
An object is a specific instance of that blueprint, (can contain multiple methods).
Here is the basic issue I am having, (I feel like it is a really dumb hang-up):
I use Eclipse to create a project by selecting File/New Java Project and name it LunchOrder.
With my newly created project selected, I select New/Class to start writing my code. I name that class LunchOrder and start coding, (I would probably also create a LunchOrderRunner class that would accept methods passed from the LunchOrder sub class).
That .java file that is created is called a class...but I can have multiple "classes" within that .java file. How can I have a bunch of classes within a class? I really feel like that .java...thing...should be called something else. How can it be a class?
I really hope someone out there understands my confusion. Please help.
Videos
What is the Object class in Java?
What is the clone() method in Java?
What is the purpose of the toString() method in Java?
Been trying to learn about OOP in java but couldnt understand what an object is and what it does. I just know how to use it to call methods.
Java 8 method references would be ideal for this - the tricky part is getting to the underlying method, because the method reference syntax itself results in an opaque lambda object.
Found this after a bit of searching:
http://benjiweber.co.uk/blog/2013/12/28/typesafe-database-interaction-with-java-8/
Neat trick - call the method on a proxy object that records the method name. Haven't tried it but looks promising.
In your method call: Method me = (new MethodNameHelper(){}).getMethod();
/**
* Proper use of this class is
* Method me = (new MethodNameHelper(){}).getMethod();
* the anonymous class allows easy access to the method name of the enclosing scope.
*/
public class MethodNameHelper {
public Method getMethod() {
return this.getClass().getEnclosingMethod();
}
}