-1

I have a user-defined class Fraction which contains several constructors and member methods. I want to create a class of separate "standalone functions" which use two instances of Fraction as arguments to either create a third Fraction or modify and return one of the instances it is passed.

class MyFractionProject{
public static void main(String[] args){
    class Fraction{
        private int numerator;
        private int denominator;
        public Fraction(){
            numerator = 0;
            denominator = 1;
        }//default constructor

       public Fraction (int num, int denom){
           numerator = num;
           denominator = denom;
       }//sample constructor

       //other constructors


       //accessors and mutators (setters and getters) are here

       public Fraction multiply(Fraction otherFraction){
           Fraction result = new Fraction(//multiply the Fractions);
           return result;
       }//sample member method, calls a constructor and accessor
       //a bunch of other member methods are here

    }//end Fraction

    //New standalone utility class goes here:
    class FractionUtility{
        //suite of ~5 functions which take two Fraction objects as arguments
        //and return a Fraction object, either one of the arguments or a new 
        //instance
        public static FractionUtility UtilityMultiply(Fraction fr1, Fraction fr2){
            //lots of stuff
        }//Does this HAVE to return a FractionUtility, or can it return a Fraction?
         //Can I declare variables outside the static FractionUtility methods if they
         //will be needed every time the method is called? Will they be accessible 
         //inside the static methods? Do I need to instead declare them inside every 
         //static method so that they're visible each time I call the method?  
    }//end FractionUtility

    //bunch of other stuff in main(), successfully uses Fraction but not 
    //FractionUtility

}//end main()
}

The Utility class is required to be defined separate from the body of Fraction. I need to have several different instances of Fractions, but never need to instantiate FractionUtility. This makes is seem like making it a static class would work, but when I do it throws errors--generally that nonstatic Fraction variables can't be accessed from a static context.

I could see how it would make sense to define both classes outside of main() and then import them, but I have no idea how to do that or what rules apply if I do.

Casey
  • 41,449
  • 7
  • 95
  • 125
Pete
  • 1
  • 1
  • The methods in `MyUtilityClass` *should* be static. This only means that you can't access instance members of `MyUtilityClass` without an instance. It does not mean that you can't access instance members in other classes, provided that you have an instance on which to access them. Please provide a concrete example of something you need to do *and* all of the members of `MyClass` that would be required to accomplish that task. As written, you don't really ask an answerable question. – cdhowie Nov 03 '14 at 20:51
  • 1
    Is that `main()` method supposed to belong to class `MyClass`? It has to belong to *some* class. If you try to compile a Java source file containing only what you presented then the compiler will certainly emit errors. – John Bollinger Nov 03 '14 at 20:57
  • Do classes `MyClass` and `UtilityClass` need to be declared *inside* the `main()` method? That's allowed, but unusual. Actually, it's allowed in *instance* methods. In a `static` method you may need to declare those classes `static`, if you can declare them there at all. – John Bollinger Nov 03 '14 at 20:58
  • Does member `MyClass.data` need to be `private`, as shown? – John Bollinger Nov 03 '14 at 20:59
  • @JohnBollinger the main method belongs to a class which is inclusive of the entire program, call it MyProgram. I'll edit to clarify that. Declaring MyClass and UtilityClass inside main() was my way of dealing with static vs. nonstatic reference problems: before the inclusion of Utility class, it all ran from within main() with no trouble. MyClass.data does need to be private, we have to prove understanding of accessors and mutators. If I wanted to declare these classes outside of main without causing conflict between static and nonstatic methods, how would I do so? – Pete Nov 04 '14 at 00:35
  • You want the general structure @Renato demonstrated for you. If you need to demonstrate understanding of accessors and mutators, then you should give class `MyClass` an appropriate accessor and mutator for its `data` member. Then either static or instances methods of class `UtilityClass` will be able to use those methods to manipulate instances of `MyClass`. – John Bollinger Nov 04 '14 at 15:40

1 Answers1

1

Well, it seems you just want to declare a couple of un-related classes in the same file.

That's what static inner classes are for.

For example, you can do:

public class Something {

static class MyClass {
    private int data = 0;

    public MyClass() {
    }
    public MyClass(int data) {
        this.data = data;
    }

    public MyClass makeNew( MyClass otherinstance ) {
        MyClass result = new MyClass( this.data + otherinstance.data );
        return result;
    }
}


static class MyUtilityClass {
}

public static void main( String[] args ) {
    MyClass myClass = new MyClass();
    MyClass copy = myClass.makeNew( new MyClass() );
    MyUtilityClass utilityClass = new MyUtilityClass();
}
}
Renato
  • 12,940
  • 3
  • 54
  • 85
  • If you don't specify the scope of some element in Java, it will be package-private by default. That means any other classes in the same package will be able to see MyUtilityClass, for example. If you want to make it visible to any other class in any other package, just make it public. If you only want them visible inside this file, make them private. The same applies for methods. Of course, class visibility prevails over method visibility, because of course if you can't even see a class that is not public, you can't call any methods on it even if they are public. – Renato Nov 03 '14 at 21:12
  • Note that the JLS terminology is "static nested" class. An "inner class" is a nested class that is not static. – John Bollinger Nov 03 '14 at 21:13
  • When I declare MyClass this way, doesn't that cause problems later when I want to instantiate it with a variety of different values, say with two instances of MyClass, one with int data == 10 and the other data == 20? And when I instantiate MyUtilityClass in that scenario, it will return a MyUtilityClass, not a MyClass, right? I'm going to edit my original post to provide more information, I was originally wary of posting too much detail about the assignment but it's clearly not going to accomplish anything if I don't. Updates soon. – Pete Nov 04 '14 at 00:39
  • Your doubts seem to be very basic, they would all be solved if you just read a beginner's Java book. There's so many of them and I can't recommend a single one, but just beware that you can't just learn programming languages by asking questions on SO... once you've mastered a couple of languages, at least, then you will probably be able to pick others up by reading just a quick reference page or a blog post, then asking about anything that seems tricky here. But until then, you should do the hard work and learn from scratch before you can write meaningful programs. – Renato Nov 04 '14 at 13:51
  • @Pete, I suppose you are confused about what it means for a nested class to be `static`. All it means is that instances of the static nested class are not associated with any particular instance of the containing class. The *members* of a static nested class are not necessarily static. Such a class can have any number of instances, each with their own independent instance data, just like any other class. You cannot declare a static nested class inside a method, however -- such classes must be declared directly within the body of their containing class. – John Bollinger Nov 04 '14 at 15:48