Results 1 to 11 of 11
  1. #1
    supernerd is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Question C++ guy learning Java: No header and source files in Java. Alternatives?

    Hello,

    I am self-taught intermediate(at best) C++ programmer and I've been spending the afternoon trying to acquire some Java basics. I want to learn Java so I can make platform independent apps and stuff. So far I am enjoying it, and have found a lot of common ground between the two languages. But there is one thing that has been driving me nuts so far:

    In C++, when creating a class, we like to keep our method(or function, depending on your preferred nomenclature) declarations and definitions separate, so we create a header file in which all methods, etc. are declared as "prototype functions", and a source file, in which all the methods are defined and implemented. This keeps the implementation details, with all its information overload, separate and hidden from the classes interface. It seems like a good method of information hiding, and it works well for me.

    However, as of yet I have been unable to discover how to do this in Java. In all the tutorials I see, they go and define and implement the class methods right there in the damned middle of the class declaration, which seems really awful. Its like some horrible form of code exhibitionism, where these ugly, fat ass, methods are standing in the middle of the street butt-ass naked, flashing their junk(hundreds and thousands of lines of it) all over the place in public, making your eyes bleed.

    Surely, in real Java code, as opposed to tutorial examples, this is not the case !? Java seems to me to be even more designed for object oriented programming than C++. So tell me Java programmers, how do you hide your method implementation from your class definitions in Java. However you handle this in Java, I imagine it is a pretty good system, but I can't seem to find any tutorials that deal with this. Also, I apologize if my question seems laughably ignorant, but of course I am going to be an idiot at Java after only a few hours playing with it.

    Thanks for reading my question, and I will be extremely grateful if anyone can provide any help, explanation, tips, etc. or can point me in the right direction of where to find the information I am looking for. I greatly appreciate anyone who will help make me less ignorant! :D

    Thank you!
    Last edited by supernerd; 04-30-2012 at 02:16 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: C++ guy learning Java: No header and source files in Java. Alternatives?

    Here's a very brief and simplistic reply, but as you know, there are no header files in Java, but exposed methods are public whereas hidden ones are private. Also, interfaces can serve a similar purpose -- publish public methods which classes that implement the interface must flesh out.

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: C++ guy learning Java: No header and source files in Java. Alternatives?

    Coming also from C++ I had the same feelings at the beginning. However there is a way we C++ guys know that works best here: self-discipline... In theory you may make a horrible mess with Java less-restricted code syntax but if you keep to some rules it looks even better than C++. For one you have with the common environments like eclipse a very good auto formatting (and I am NOT a fan of those tools usually) and the purely object orientated approach has a very nice feeling once you got used to it.

    Some rules that I apply to myself:
    - All variable declarations of a class should go right at the beginning of the class, no methods above.
    - No method should have more than two pages, else there is room for improvement...

    My typical class looks like that - just my 2 cents, it might not fit the rules of someone else around the java community in some points though...

    Java Code:
    //==================================================================================================================================
    // Main
    //----------------------------------------------------------------------------------------------------------------------------------
    // - Main entry point for the application
    // - Blabla
    //==================================================================================================================================
    public class Main
    {
    	static int				_iWaitCounterMax	= 3;		// Max. number of loops to delete lockfile and then terminate if no ops is possible
    	static FileLog			_oLog				= null;
    	static SimpleDateFormat	_oDateFormatFile		= new SimpleDateFormat("yyyyMMdd");
    	static SimpleDateFormat	_oDateFormatRunfile	= new SimpleDateFormat("yyyyMMdd hh:mm:ss");
    
    	final static String			FILE_CONFIG		= "autoplot.cfg";
    	final static String			FILE_LOG			= "autoplot.log";
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] sArguments)
    	{
            }
    }
    Last edited by Sierra; 04-30-2012 at 10:03 AM.

  4. #4
    supernerd is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Talking Re: C++ guy learning Java: No header and source files in Java. Alternatives?

    Quote Originally Posted by Sierra View Post
    Coming also from C++ I had the same feelings at the beginning. However there is a way we C++ guys know that works best here: self-discipline... In theory you may make a horrible mess with Java less-restricted code syntax but if you keep to some rules it looks even better than C++. For one you have with the common environments like eclipse a very good auto formatting (and I am NOT a fan of those tools usually) and the purely object orientated approach has a very nice feeling once you got used to it.

    Some rules that I apply to myself:
    - All variable declarations of a class should go right at the beginning of the class, no methods above.
    - No method should have more than two pages, else there is room for improvement...

    My typical class looks like that - just my 2 cents, it might not fit the rules of someone else around the java community in some points though...

    Java Code:
    // Pretty code and stuff ....
    Thanks for your very helpful reply Sierra! It's nice to hear from someone with a C++ background.

    Placing the variable declarations before the methods is certainly neater, but it is somewhat backwards to me; I like to have the most public things at the top, and data items, such as variables, are almost always private. Still, better than having to read past all the methods first to get to the variables.

    Also, I agree about method length. The things is, often times in my classes I will have multiple methods: constructors, access routines, and numerous other methods depending on what I'm specifically modelling with my class. This all adds up. Next thing I know, I have a bunch of short, pretty, and elegant routines, each of them individually great, but cumulatively making a mess by the shear quantity of them. If I could have a header file to show the basic structure and interface of my class, and keep all the mess contained and organized in the source file, it wouldn't be an issue.

    It's the same reason I keep my clothes hanging in my closet, and not in a pile in the middle of my bedroom floor. When I need them, I go to my closet, and find them via whatever arbitrary organization scheme I have set up. Otherwise they are out of the way and out of sight. Your suggestion, while helpful, is like me moving the clothes pile on my floor to the back of the room, so the stuff that takes up less space and I use more is closer. I can make that work if I have to but I would prefer a closet.

    Sorry if it sounds like I am complaining; that is not intentional. Your post was good, helpful, and well thought out. I am sure I will get better at writing pretty Java with practice. I'm just trying to better express my trouble. C++ is all I currently know so different languages' approaches to doing things seem weird and alien, but it will be good for me to expand my mind and learn new approaches.
    Last edited by supernerd; 04-30-2012 at 11:50 PM.

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: C++ guy learning Java: No header and source files in Java. Alternatives?

    Actually I can say that you should just try and find out. There is certainly several things you stumble over when changing to a new programming language and almost no change is never appreciated by anyone who is used to something.

    For Java you will probably find a whole lot of new ways to do things. You should be aware not to try to force known concepts into the new environment where might be other means of doing things. Actually I tend to pile my clothes in the bedroom - however... that is why I chose Eclipse as environment, as it provides such features as a quick overview over the classes and methods. It also provides a good navigation to references, declarations and such. Well actually you have to try it, I am sure you will get to like it. If you have a specific problem or challenge just 'complain'. Maybe there is a way the java people know to do it and you do not yet.

    I found e.g. the concept of interfaces very interesting (as Fubarable said) as it is something that C++ does not have that way. Take a look into it. A well known drawback of Java is that generally it "does not like" hardware related programming. :)
    I like likes!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: C++ guy learning Java: No header and source files in Java. Alternatives?

    There is also the API documentation of a class which is designed to expose the information that is useful for the class's *user*.

    It takes a little getting used to the fact that API documentation is generated from within the file containing the implementation rather than in a separate header file. But, once got used to, it's an entirely reasonable arrangement since any change to a class that affects its external, "visible", behaviour ought to be documented there and then. Unless a method/function signature changes it is all too easy to put off changing a separate file until some "later" that never eventuates.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: C++ guy learning Java: No header and source files in Java. Alternatives?

    C was created at a time when development was done using really basic tools, essentially text editors.
    Separating header from body helped to make the code easier to follow and act as a form of API for whatever lib you were using at the time (think stdlib), as well as a point for linking.

    That was 40 years ago.

    Java was written with IDEs being far more prolific and so the need for headers as an organisational thing was less necessary. Indeed they would be an unecessary complication. After all, any decent IDE will give you access to the API for a class, as well as an outline facility which lists all methods and attributes in whatever order you want.

    Now, having said that, standard Java coding practice is to code against interfaces. The interface defines the contract an object has, and classes implementing the interface hold the code. Look at the Collections framework in Java for an example. Note, this is not the same as headers since interfaces have a different function.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: C++ guy learning Java: No header and source files in Java. Alternatives?

    Header files are the thing I always disliked about C++. Your closet analogy isn't really accurate, it would be more like having a very large closet then having pictures of all the clothes in it in a drawer so you look at what you want in the drawer then go to the cupboard to get it. It's old fashioned and pointless these days.

    If you use Eclipse you'll find the 'Outline' view shows you all methods in a C++ header style without needing those ugly additional files. If you're classes are properly documented i.e.

    Java Code:
     /**
      * This class does something
      * @author Me
      * @version 1.0
      */
      public class SomethingDoer{
      }
    then hovering over methods/classes or viewing the autocomplete set during use gives you that full description of each method/class
    Last edited by k1ng; 05-01-2012 at 03:05 PM. Reason: fixed bad spelling of analogy

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: C++ guy learning Java: No header and source files in Java. Alternatives?

    Don't forget that the 'old' compilers can't inspect already compiled code while a Java compiler can do so. The 'old' compilers do need those .h (include) files while the Java compiler can simply inspect the already compiled code to check if it is called/referenced correctly. There is no use for .h files for the Java compiler.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    supernerd is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: C++ guy learning Java: No header and source files in Java. Alternatives?

    Thank you so much everyone for your replies!

    My original post was kind of a panic reaction against a different approach to organization. Since then I have taken a little more time to do some research and read some Java code as I should have done before freaking out. The suggestion of Java's Interface thing (thanks Fubarable, Sierra, and Tolls) was what I was looking for. I now better understand and better appreciate Java's approach to this.

    It was also informative to hear about why C++ is uses the header file system, and why its outdated, so thanks pbrockway2, Tolls, k1ng, and JosAH.

    I should also point out that I am fairly new to programming, and perhaps 'intermediate' was too high of a ranking to apply to myself. Intermediate beginner would have been more appropriate perhaps. So far I have only programed recreationally(I'm a student) not professionally, but I plan to go into computer science applied to scientific modeling, either in theoretical physics or chemical physics(computational chemistry).

    Also, I have almost no experience with IDE's. I've tried Eclipse before but not enough to get used to it. I guess I should get around to learning an IDE; I'm just used to using a text editor by force of habit.

    So my intent was not to say Java's way of doing things was inferior to C++ or even less than superior to C++'s. It was just my initial perception, based on limited knowledge and experience. Thanks for sorting me out and putting up with my silliness. I learned quite a bit from just this short thread.

    I suppose this thread is solved but if anyone else has any comments, suggestions, or knowledge they feel like sharing, I would love to hear it.

    Thanks for making me less ignorant!
    Last edited by supernerd; 05-03-2012 at 10:08 PM. Reason: stupid typos

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: C++ guy learning Java: No header and source files in Java. Alternatives?

    Stick with the text editor (I would recommend Notepad++ because it adds syntax highlighting, which is handy) at least until you are comfortable with things like the classpath and the general structure of Java code and how all the bits (javac, java, those sorts of commands) all fit together. Then you'll know, when you start with an IDE what it is doing for you. We get a lot of questions here from people who go straight to an IDE and don't understand how the classpath works and what is actually going on under the IDE hood, things they would have learnt had they started on the command line.

    Interfaces, by the way, were Javas mechanism for allowing multiple inheritance (at least of contract) when you can only extend from a single parent class. They have since become more entrenched with the concept of coding against interfaces, which allows you to drop different implementations of an interface into your code. For example the Collections framework, whereby you might define a method as returning a List<SomeClass>, which allows you to change the underlying method to return an ArrayList or LinkedList or whatever without requiring you to change the calling code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Source for learning MVC.
    By fatabass in forum New To Java
    Replies: 2
    Last Post: 03-10-2012, 09:40 AM
  2. Best Site for Java beginers to learning java
    By pranitha63 in forum New To Java
    Replies: 0
    Last Post: 03-08-2011, 10:21 AM
  3. python source to java source converter?
    By conor147 in forum New To Java
    Replies: 0
    Last Post: 01-15-2010, 05:14 AM
  4. How to Compile JavaScript source into Java class files
    By tosreejith in forum New To Java
    Replies: 0
    Last Post: 04-06-2009, 03:12 PM
  5. Java source files security
    By sunilpatel28 in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2008, 12:17 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •