LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-29-2006, 05:46 AM   #1
Orkie
Member
 
Registered: Mar 2005
Distribution: Breezy Badger
Posts: 248

Rep: Reputation: 30
Java: Placing class members in separate files


I've just started looking at Java but I can't find any way of placing members of a class in separate files from the main bulk of the code for that class (when they grow too big to keep all in one file). Currently, my main class is just going to keep expanding forever. In C/C++, I would when things started to get a bit bloated just split it down into separate files and use #include to link it together but this doesn't seem possible in Java.

Thanks
 
Old 01-29-2006, 06:22 AM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
A well designed object-oriented program will have many classes and each class should be simple and abstract and only need to take care of a limited number of things that is relevant to the concept represented by that class; there is no reason that you should have a "main class that is just going to keep expanding forever".

Last edited by spooon; 01-29-2006 at 06:24 AM.
 
Old 01-29-2006, 07:00 AM   #3
Orkie
Member
 
Registered: Mar 2005
Distribution: Breezy Badger
Posts: 248

Original Poster
Rep: Reputation: 30
I need to squash everything into one class because Java seems to be fussy about static members accessing non-static ones unless they are in the same class (though I may be misunderstanding this). I'll go back and see if I can restructure some things though.
 
Old 01-29-2006, 07:19 AM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
Static things shouldn't be able to access any non-static things of the class. Can you show an example?
 
Old 01-29-2006, 07:38 AM   #5
Orkie
Member
 
Registered: Mar 2005
Distribution: Breezy Badger
Posts: 248

Original Poster
Rep: Reputation: 30
No, because I was misremembering, sorry I've done some more research and it seems that I need to create an instance of the class and use that to access non-static members. Is this correct? If so, I can just do that and it will solve all my problems.
 
Old 01-29-2006, 05:06 PM   #6
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
Quote:
Originally Posted by Orkie
No, because I was misremembering, sorry I've done some more research and it seems that I need to create an instance of the class and use that to access non-static members. Is this correct? If so, I can just do that and it will solve all my problems.
Yes. Do you understand the concept of classes? By definition, non-static members are specific to an instance of a class, so you need to have instances of the class in order to have non-static members exist within those instances. Different instances have separate non-static members.

Static members are completely different and always exist in a kind of global way outside of any instances and regardless of whether there are any instances. The main() method that is required to start Java apps is declared static, because at that point there exists no instances of any class; to do stuff you usually create instances of various classes.
 
Old 01-30-2006, 02:35 PM   #7
Orkie
Member
 
Registered: Mar 2005
Distribution: Breezy Badger
Posts: 248

Original Poster
Rep: Reputation: 30
Ah, I see. So static things in Java mean exactly the same as they do in C. That makes it clearer. Thanks for the help.
 
Old 01-30-2006, 03:11 PM   #8
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
my two cents...
the way I understand Java static members, such as member functions, is that it gives you the same type of result as putting functions in a namespace. if you define a static function called func1() in a java class called Class1, then you can call the function with

Class1.func1();

without creating an instance of Class1. this allows you to have multiple function named func1, you could call something like this.

Class1.func1();
Class2.func1();

and have the functions do different things, which is very similar to the way you would call functions in different namespaces from C++.

namespace1::func1();
namespace2::func2();

again, this the way i think of how static functions are useful, comeing from C/C++ first.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script finding files, placing them and naming the TroelsSmit Linux - Newbie 4 11-18-2004 10:50 PM
script finding files, placing them and naming the TroelsSmit Linux - Networking 0 11-18-2004 03:21 PM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 12:52 PM
Running Java Class Files hackersapien Programming 9 08-07-2003 07:42 AM
can't run java class files in linux JIV Programming 6 03-25-2003 02:20 AM


All times are GMT -5. The time now is 09:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration