• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help with my understanding of classes in Java and API and many other aspects of Java  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between having a class without a main method Vs with a main method Vs a class with a main method outside of the class but within the same package? I could be mistake but I think I"e seen Java programs with main methods, how does that work if so.

Also, am thinking OOP is more like class oriented programming because everything derives from the class right? When we start making our code from scratch we introduce classes right in left it's impossible to write a class that is not composed on other classes. Was wondering is this true in everything single thing in Java, everything is based off this structure of classes building off more basic classes. Like I read that the Object class is like the big daddy class of all the other classes, all classes derived from big daddy Object class.

Does the Object class has a main method in there? is it composed of any smaller base parts?

Also String is a class, does it also have a main method, does it have a field composed of static and nonstatic members? I know it has methods because I've used them before. Does it have a constructor?
Do all classes have constructors? or at least most of them?


What about interfaces like Iterable, Comparable, Comparator, and Clonable are al those are classes too? do they have a main method? a constructor? fields composed of static and nonstatic members?


What about Collections Map, List, Set are they classes too? same questions as before. I know they have derivatives like HashMap, LinkedList, HashSet, would those be considered their subclasses?

And can you not use Map, List, Set directly? Is it only when you make a derivative of it that it actually does stuff?

For example:


but if we wrote:



and couldn't we also do



And Collection, that's like the big daddy of the Collections framework? so that would work because Collection can instantiate any one of its sub member classes
So collections work from general then once we instantiate we make it specific? is that right?
Another question is, is it correct in saying that Map is a subclass of Collection and that HashMap is a subclass of Map and a subclass of a subclass of Collection?


And finally, API, I know that there are these things which we can import from and get all this stuff, I think API is like a big group of classes and all those classes of course come with their own methods. So when we import an API like the two I've seen are java.awt; and java.swing; when we import them we get access to a ton of more classes and therefore methods. So an API is like a way of extracting a lot more functionality into your code that you didn't have access to using/adding.

My intuition tells me that all of the primitive data types, non primitive data types (like String), Interfaces, Collections, and API all are actually classes and that each has a main method, constructors, fields composed of static and nonstatic variables.

Grateful for any help on understanding
Thank you
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! A lot of questions suggesting that you need to get a good text and do some reading.

On the main() method.  It is the ONE static method that the JVM looks to execute when the name of the class containing the main() method is referenced with the java command:
java TheClassWIthMain

Otherwise it is just another static method that can be called like any other static method:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have way too many questions rolled into this one post. It's better if you keep one thread per question, that way the thread and the responses will be more focused.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since a response to the main() question has already been given, let's just keep responses in this thread limited to main().

main() is the entry point of a program. You normally have only one entry point to an application so most classes that you write will NOT have a main() method.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the main method is the entry point to an application, are you going to start an application off from inside a class like String? How is String to know what you want to do? Have you been through the API documentation to see how many classes have main methods declared? You write a main method because you are going to start an application off. You decide what that main method will do and which methods it starts. You use the types provided by the API as building blocks to make up your app.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If the main method is the entry point to an application, are you going to start an application off from inside a class like String? How is String to know what you want to do? Have you been through the API documentation to see how many classes have main methods declared? You write a main method because you are going to start an application off. You decide what that main method will do and which methods it starts. You use the types provided by the API as building blocks to make up your app.


Thank you

I am sorry that I don't understand. I know that we need main because it starts off the program, that's where all things get executed, or where the magic happens by connecting all the stuff we implemented above the main method. Was confused by: "are you going to start an application off from inside a class like String? How is String to know what you want to do?".


Also I was looking at swing, want to know if I understand this structure correctly.

Swing is a framework, a framework as far as I can tell is just a warehouse to store pre-made interfaces and classes that we didn't have access to beforehand, to add another level of functionality to the code.

An "interface" is just a warehouse of prototype methods that are to be implemented once your class uses the implement keyword.
And the "classes" that the framework holds onto are composed of fields, constructors, and methods. And I guess we'd use them much the same way we use classes like String and its methods.

Why would one choose using a class over and interface? aren't interfaces composed of blank methods whereas the class is fully implemented? wouldn't having a class full of optional methods always be better?

Also, how does a framework differ from an Application Programming Interface? they seem very similar.

Thank you again.

 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . . I am sorry that I don't understand. I know that we need main because it starts off the program, . . .
Sorry for not being clear.
Yes, you need to write a main method, usually one per application, and it is called automatically by the JVM. Yu can read more about it here.
The classes in the API are not intended to be applications; I searched through the M index and didn't find any with a main() method.

You have to decide for yourself what your app will be and where it will start, and you write a main method there.
The classes in the API are not the building; they are bricks and beams and door and windows. You have to put them together yourself.
Why would one choose using a class over and interface? aren't interfaces composed of blank methods whereas the class is fully implemented? wouldn't having a class full of optional methods always be better?
That is going to take a long time to answer, but try starting off with this FAQ. See whether that helps.
Also, how does a framework differ from an Application Programming Interface? they seem very similar.
I think the number of different answers you get to that sort of question is equal to the number of people asked
I think that a framework is part of an API and a framework is designed for a particular purpose. For example, the Swing classes are a framework intended for creating GUIs.


Thank you again.

Our pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!