1

I hate to ask something so trivial, but I can't work this one out. I'm trying to create a basic object for convenience, like this:

triangle = {
  side: { A: 0, B: 0, C: 0 },
  angle: { a, b, c },

  function calcAngle(){}
}

Ideally, I'd like to just create a generic object on the fly. I'm only creating one "triangle", must I create a whole class for one instance? I'm sure this is answered somewhere, but I can't seem to word the question right for anything useful. For your amusement I'll post some of my failures:

public class TGUI{

// Attempt One
   public Object triangle = new Object(){ int test; };
   public static void main(String[] args) {
     triangle.test = 1;
     // ^- Cannot make a static reference to the non-static field triangle

     triangle tri = new triangle();
     // ^- Made the compiler cry; triangle cannot be resolved to as type
   }

// Attempt Two
   public class triangle{ int test; }
   public static void main(String[] args) {
       triangle tri = new triangle();
       /* ^- No enclosing instance of TGUI is accessible.  Must qualify the allocation with an enclosing instance of type TGUI (eg x.new A() where x is an instance of TGUI) */
   }

// Attempt Three
   public void triangle(){ int test = 1; }
   public static void main(String[] args) {
     triangle tri = new triantle();
     // ^- triangle cannot be resolved to a type
   }

// Attempt Four
   public TGUI(){ int test; }
   /* I'm gonna quit here, you get the idea */
}
4
  • Could you just use an array of three double values? Commented Jan 29, 2014 at 19:32
  • You need to define a Triangle class. And, generally speaking, it should not be nested unless you have a good reason to make it so. Commented Jan 29, 2014 at 19:35
  • I could make separate arrays for the sides and the angles, but I like the organization of x.side.a; x.angle.b instead of side[0]; angle[1]; I guess that's the way to go though. So, the right way to do it is to define a new class in a whole separate file just to use the object once? Maybe Map is just the way to go. Commented Jan 29, 2014 at 19:40
  • 1
    You always have the option of leaving it in JSON form. You lose the "classiness" of being able to invoke methods on it, but the data values remain accessible in a fairly meaningful way. Commented Jan 29, 2014 at 19:42

4 Answers 4

2

Ideally, I'd like to just create a generic object on the fly.

Then you shouldn't use a statically typed language.

The only ways to do this in Java are

  • use reflection (not what you want), or
  • use a JSON library,
  • give up typing entirely and use a Map<String, Integer>.

In your case, sounds like you just need an actual Triangle class though. Java requires lots of typing, better to get used to it than write bad code to avoid it.

Sign up to request clarification or add additional context in comments.

Comments

1

Attempt 2 was closer. You needed a nested static class:

public static class triangle{ int test; }

(or triangle can be in a separate file).

It's still far from how Java operates on its static type system.

Comments

1
public class Triangle {
    double sideA;
    double sideB;
    double sideC;
    double[] angles = new double[3];
    double calcAngle() {
        something;
        return somethingElse;
    }
}

Comments

0

indeed, you should use a JVM-based script language like Groovy to implement such dynamic things out of the box. You would create an instance of MetaClass and add all your fields on the fly. Another option for good ol' Java only would be to create a simple (marker?) interface and implement it as an anonymous inner class just where you need it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.