#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2

    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.

    Thank You!
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2
    Originally Posted by bullet
    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.
    Oh ok, thank you.

    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?

    Also :-
    As I asked earlier, is there any classic books for java like K&R for C, bjarne's C++ etc?
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2
    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?
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    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.
    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.
  12. #7
  13. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2
    Originally Posted by bullet
    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.
    Thank you!

    Originally Posted by rodiongork
    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.
    I have a program to write in java soon. After finishing it I'll pick up a book. Anyways thank you!

IMN logo majestic logo threadwatch logo seochat tools logo