• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

re-write a java file that contains multiple classes


Hi All,

I have a java class which contains classes within that class.

Like this:
public class myClass()
{
int num;
class subClass()
{
num = 4;
}
System.out.println(num);
}

subClass uses variables which myClass also uses.

I want to re-write this java class and seperate all of the classes into new files.
I'd like some pointers/tips to achieve this.

Am I right in thinking that having multiple classes within one file is a bad practice?

Thanks
0
monitorscreen
Asked:
monitorscreen
4 Solutions
 
hoomanvCommented:
// MyClass.java

public class MyClass
{
    public int num;
}

// SubClass.java

public class SubClass
{
    private MyClass mc;

    SubClass(MyClass mc) {
        this.mc = mc;
    }
   
    void f() {
        mc.num = 4;
    }
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> num = 4;

You are not declaring a data-member there - you are assigning a value to an existing variable, so this is a "statement". Hence, it needs to be written in a method.

>> class subClass()

You don't give () in class definitions.
0
 
hoomanvCommented:
> Am I right in thinking that having multiple classes within one file is a bad practice?
Java allows you to define more than one class in a file, as long as there is exactly one public class whose name matches the name of the file.
this can be good for small classes that dont need to be accessible outside the package,
having multiple classes in one file could reduce the code readability
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mayank SAssociate Director - Product EngineeringCommented:
>> as long as there is exactly one public class whose name matches the name of the file.

Nested classes can be public too, but at the top level there can be only one 'public' class.
0
 
Igor BazarnyCommented:
> Am I right in thinking that having multiple classes within one file is a bad practice?
It depends. Sometimes you nested classes are local to enclosing class and no other class is interested. Sometimes they are small and heavily connected to enclosing class. Sometimes you don't want to pollute package namespace with names specific to certain context. Sometimes you just need to register listener for events, forwarding them to enclosing class method.

There is a rule about multiple _top-level_ classes. It is a good practice to name java files by classes they contain, no matter whether that classes package local or public. IMHO, this rule doesn't apply to nested classes becauses it focuses on easy way to get source file from the class name, and for nested class you always have enclosing class name.

Sample by hoomanv is correct, but I would start rewriting only if it helps me and others to understand source code, e.g. if there are too many nested classes, or thea are too large.
0
 
mark1348Commented:
You might want to add a few more constraints on who can access and change your data members (like "num").  Youhave MANY options here and you should do a little research on java terms like "public", "protected", "private", and "package protected".

Working with the good example given to you by hoomanv:

// in a file called "MyClass.java"
public class MyClass
{
    // private means no direct access is granted to other classes
    private int num;

    // allows other classes to get the value of num
    public int getNum()
    {
        return num;
    }
   
    // allows other classes to set the value of num
    public void setNum(int inNum)
    {
        num = inNum;
    }
}

// in a file called "SubClass.java"
public class SubClass
{
    private MyClass mc;

    SubClass(MyClass mc) {
        this.mc = mc;
    }
   
    void f() {
        // note that the "setNum()" method is called here
        // because there is no direct access to num from other classes
        mc.setNum(4);
    }
}

You could also consider inheritance which is very powerful but should be used carefully.  Inheritance would look like this:

// in a file called "MyClass.java"
public class MyClass
{
    // protected means direct access to "num"is granted only to child and package classes
    protected int num;
}


// in a file called "SubClass.java"
// The "extends" keyword in the class decleration that flags SubClass is a child of MyClass
// SubClass is a child class of MyClass so it has direct access to any
// "public" and "protected" variables and methods.
public class SubClass extends MyClass
{
    SubClass() {
        super(); // calls the default constructor for the parent class (MyClass)
    }
   
    void f() {
        num = 4; // child class has direct access to parent's "protected" members
    }
}

The example by hoomanv is good and is the most simple,but these others might also help if your situation is a little more complicated.  It is also worth noting that having more than one class in a file is not always bad, as stated by both hoomanv and bazarny.  You just need to give thought to what your class is supposed to do and what it will need to get that done.  The more it can stand on its own the fewer connections it will need to other classes.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now