This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

application with multiple files?

 
Ralph Kehmeier
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks;

I am writing a Javabean / JSP application and have a problem, maybe because I'm thinking like a C programmer. I would like to separate my application into multiple Java source files.

Side Question: can a single Java class be divided into two source files?

Specifically, I have written a utility function which I would like to use in multiple places in the application. If it were a C program, I would put the function in a 'utility.c' file, make a 'utility.h' header file, call the function whenever needed, and live happily foreverafter.

Main Question: what is the simplest way do the equivalent of the above in Java?

Thanks,
Ralph
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing a web application before you have a solid grounding in Java is a bad idea. Really bad. But to answer your question: invoke the method whenever and wherever you need to:

TheClass theObject = ... //create a new instance or get a reference to an existing instance
theObject.theMethod(theArguments);
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know there are no equivants to a header file in java. Implementation + everything else is shoved into a .java file.

It does take a bit getting used to. The Java way felt weird to me for a while.

You can make the method(for some reason Java people insist on calling functions, methods) static. Then you can call it directly with ClassName.methodName();
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, a method is defined to as static or not for compelling logical reasons, not just because one feels a need to "call it directly".
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rusty Shackleford:
As far as I know there are no equivants to a header file in java. Implementation + everything else is shoved into a .java file.

It does take a bit getting used to. The Java way felt weird to me for a while.

You can make the method(for some reason Java people insist on calling functions, methods) static. Then you can call it directly with ClassName.methodName();


In the context of Java they are methods, calling it a function is erroneous. Of course it's a pretty safe bet most people will understand exactly what you mean, but your comment is rather presumptuous. Afterall, why do you C/C++ people insist on calling subroutines 'functions' anyway? I can only imagine what the mathematicians think of our so-called functions.
 
Ralph Kehmeier
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you for your responses. But Jeff, after reading yours, I felt like crying. "Writing a web application before you have a solid grounding in Java is a bad idea. Really bad." I'm sorry for being a beginner! Where do I get an official license to write web applications? Omigod, could something, like, explode if I make a mistake??

I infer from your answer that in Java it is indeed necessary to invent some lame class in order to execute a simple little utility function.

No one answered my first question. Can a single Java class be divided across two source files?

Thanks,
Ralph
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was answered.

No

Unless you want to split up a class into 2, which would probably make no sense, and you still will have nothing like a header file.

Interface details(not java interface) and implementation are mixed in a .java file.

A workaround is a static method, and as correctly noted should only be used when it makes sense.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph, your attitude isn't going to win you friends here. If you know so well how the world should work and it's the way you're used to in C++ why don't you stick to that?

Your question was answered quite clearly and you were given a very sound warning about not diving into advanced stuff before you understand the basics.

Did you go and write a multithreaded database server in C++ before you even know what a pointer was? I guess not.
Yet you're trying to write a multithreaded application in Java when you don't even know the core language mechanisms...

and yes, an application can exist in multiple files. But a single class can not.
I'm now working on a web application that exists (at the moment) of some 2200 sources for example.
[ June 02, 2006: Message edited by: Jeroen T Wenting ]
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,

@all
ralph is mentioning c, not c++. so i think we are asuming too much oo experience.

furthermore - he obviously is not a native speaker, same goes for me. translating his english post back to german, i admit that it's not really friendly, but it's also far away from being rude or so. we do speak like this in germany :-)

and hey, it's the beginner forum, isn't it?

@ralph
i think the problem is that you are mentioning two issues which are on the opposite sides of the range of knowledge. while (enterprise?)bean / webapp / jsp certainly is advanced, splitting a class in two files certainly is not.

at least not from a java perspective, because we (try to) think in objects and its responsibilites. if a class is longer then 100 - 200 lines, it's usually a so-called "bad smell" and we try to refactor the class (read: object) to something shorter (read: remove responsibilities).

probably it's a good idea to familarize yourself a bit with java and oo-design, before building a webapp. but hey, it takes a weekend to work yourself through some pages of a online-tutorial or a book. i see "head first java" recommended many times in this forum...

best regards and greetings from berlin,
jan





@ralph. no, nothing will explode if you make a mistake in your webapp.
 
Ralph Kehmeier
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[My] attitude isn't going to win friends? Well, perhaps I did overdo it a bit. But I found the response: "Writing ...before you have a solid grounding ...is a bad idea. Really bad." to be silly, and a bit patronizing. I tried to respond with humor. (Question: do ranch hands have a sense of humor?) After all, if Mr. Albertson is correct, why is there a "JavaServer Pages for Dummies"?? In my career as software developer, I have learned 80% by doing, 15% through reading other people's source code (good and bad), ~4% from how-to books, and now, 0.01% by consulting experts.

Rusty, I can't see anywhere my first question was answered, until you answered it ("no"). Thanks for the clear answer.

Jan, thank you for your nice response, and the concept of refactoring. Actually I am a native English speaker. Gee, is my writing that bad?! But you are half right. Greetings back to you from Wermelskirchen.
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ooops, obviously it's my english which is bad...

really thought you are using the same german-to-english-style i do. and you last name is as german as possible... :-)

cheers,
jan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic