September 26th, 2013, 12:43 PM
New to java - classes, packages, interfaces etc
I'm starting java. My previous langauges are C, C++ and python.
There are somethings that I don't understand with java.
1) I have to name my source file same as the class name which is strange.
2) Because of 1) I guess you can't make more that one class in a file.
3) There has to be a main in a class otherwise it doesn't compile. If this is the case then how do I create a class for a linked-list? I just want to have insert and delete functions, no main but java won't allow this.
Other doubts are regarding syntax and concepts like package, interfaces etc. Recommend me a classic book for java like K&R for C, bjarne stroustrup's C++ etc.
September 26th, 2013, 01:19 PM
You can have more than one class definition in a file, but only one of them can be declared public.
Java does not require you to have a main method to compile.
You must have a main method to run.
September 26th, 2013, 01:34 PM
Oh ok, thank you.
Originally Posted by bullet
I could get this in a book but still ask here:-
When I import a package will all the classes and its method be available to call or is there something like interface which I have to make?
As I asked earlier, is there any classic books for java like K&R for C, bjarne's C++ etc?
September 26th, 2013, 02:10 PM
When you import a package, you have access to all of the .class files in that package. For example, if you import java.util.*, then you have access to Scanner, Map, Set, etc.
A couple of books I know that are pretty good are the ones by Deitel and Deitel and Daniel Liang.
October 1st, 2013, 11:33 AM
A java doubt. I'm asking here only instead of creating a new one.
Java seems to be very strict of static functions. I created a main function but didn't put it as static and it gave me error. So I put every function as static then also it gave me error.
What is with static functions? Why only main can be static and not others? What is static anyway?
October 1st, 2013, 01:08 PM
You simply done something wrong. Any method could be static (though runnable entry point should be static void main with String array as argument)
I recommend you stop at once and lay your hands on Cay Horstmann's book. It is classic.
You could not really do with java by asking all the things. Language has steep learning curve, so to start comfortably and not die in frustration you really need to do this by a kind of good tutorial rather than by occasional experiments.
Java is not hard, but at the beginning it gives you too much rules and ideas to remember.
October 1st, 2013, 01:11 PM
The main method is the entry point of every application. It is the method that is executed when the program runs.
It is required that the method be declared static.
A static member of a class belongs to the class and not to any particular instance of a class.
An instance member (variable or method) can only be accessed by an instance of the class.
However, the static variables (called class variables) are shared among all instances.
There is only one copy in memory of each static variable, whereas every instance has its own copy of its instance variables.
October 1st, 2013, 01:20 PM
Originally Posted by bullet
I have a program to write in java soon. After finishing it I'll pick up a book. Anyways thank you!
Originally Posted by rodiongork