Go Back  DVD Talk Forum > General Discussions > Tech Talk
Reload this Page >

Java organization (factoring) help

Tech Talk Discuss PC Hardware, Software, Internet and Other Technology

Java organization (factoring) help

Old 07-20-04, 11:03 AM
  #1  
DVD Talk Legend
Thread Starter
 
Join Date: Oct 1999
Location: |-|@><0r [email protected]|)
Posts: 17,214
Likes: 0
Received 0 Likes on 0 Posts
Java organization (factoring) help

I'm fairly new to Java, and still trying to get used to its package structure.

I'm trying to design a set of classes that can implicitly access the elements of a container (utility) class. This is an organizational issue - how can I structure the code across classes and files? Of course, I can do this with inner classes, like this:

class A {
    int x;

    class B {
      public ChangeX() {
        x = 12;

      }

    }

    class C {
      public ChangeX() {
        x = 14;

      }

    }

}

...such that each class can implicitly access the members of the container class. But the problem with this is that every single class has to go in the same file, because you can't have a class in one file that is an inner class in another file. So I'm building this GIGANTIC file, and it's becoming unwieldy.

I realize that I could get around this by just building an interface into A to every element, but this will introduce tons of lexical junk: every time I want to use anything in A, I'll have to refer to A (for static members) or an instance of A (for instance members.)

In C++, this would be easy - I'd just expose a bunch of methods and use a suitable #include scheme. I'm not sure I can do this in C#/.NET, but I'm certain that it's difficult in Java.

Any ideas? Thanks.

- David Stein
Old 07-20-04, 11:48 AM
  #2  
DVD Talk Legend
 
Shazam's Avatar
 
Join Date: Jul 1999
Location: Canuckistan
Posts: 10,027
Likes: 0
Received 0 Likes on 0 Posts
Short answer: You can't do exactly what you want. Java doesn't provide anything like the C++ #include directive.

You could just create separate classes that are part of the same package your main classes are in.
Old 07-20-04, 12:16 PM
  #3  
DVD Talk Legend
Thread Starter
 
Join Date: Oct 1999
Location: |-|@><0r [email protected]|)
Posts: 17,214
Likes: 0
Received 0 Likes on 0 Posts
Originally posted by Shazam
You could just create separate classes that are part of the same package your main classes are in.
Yeah, that's the logical answer, and the way that it's typically done. The problem is that every class has to explicitly reference another class to use its methods. I'm building one mega-class that offers a ton of tools that the rest of my code will use, so my code will be filled with references to this utility class. It's the difference between this:

Widget w = new Widget(UtilityVariable, GetGadget(x), UtilityConstant_LeftBorder);

and this (presuming that "u" is the name of the utility class or an instance of it):

u.Widget w = new u.Widget(u.UtilityVariable, u.GetGadget(x), u.UtilityConstant_LeftBorder);

That's five class references in the same line of code. That's ugly.

I realize that my intended design is now considered bad programming design - a variation on "heavy reliance on global elements" that makes code hard to maintain. But this is a hobby project, and only I will be working on it. (And, I intend to build other projects into that same framework later, using the same set of globals.)

- David Stein
Old 07-20-04, 03:26 PM
  #4  
DVD Talk Legend
 
Shazam's Avatar
 
Join Date: Jul 1999
Location: Canuckistan
Posts: 10,027
Likes: 0
Received 0 Likes on 0 Posts
You can't do that in
Old 07-20-04, 04:57 PM
  #5  
DVD Talk Legend
Thread Starter
 
Join Date: Oct 1999
Location: |-|@><0r [email protected]|)
Posts: 17,214
Likes: 0
Received 0 Likes on 0 Posts
Originally posted by Shazam
Java 1.5.x will allow that syntax.
How? Sample code?

- David Stein
Old 07-20-04, 06:16 PM
  #6  
DVD Talk Legend
 
Shazam's Avatar
 
Join Date: Jul 1999
Location: Canuckistan
Posts: 10,027
Likes: 0
Received 0 Likes on 0 Posts
Check out "static import"

http://java.sun.com/developer/techni...leases/j2se15/

Whoops, looks like they've scaled back that functionality. I know Sun talked about allowing any static methods of a class to be referred that way, but it looks like they've removed that. Ah well.

Last edited by Shazam; 07-20-04 at 06:18 PM.

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 On
HTML code is On
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Thread Tools
Search this Thread

Archive - Advertising - Cookie Policy - Privacy Statement - Terms of Service - Do Not Sell My Personal Information

Copyright 2018 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.