ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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".
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.