Too many variables in Java? [closed]

I'm still fairly new to Java, but I personally feel my code is sloppy, having way too many variables. I know I could hide them in methods or classes, but I heard that putting them in a separate class is bad practice and most of them aren't static. Can anyone give me some tips? Thanks in advance!

public static float c = .2f; //cube dimensions private float separation = 4.0f*c; private float angle = 0f; //set to 0 private float rotate = 1.0f; private float h_height = 1.0f; private float h_width = 1.0f; private float h_length = 1.0f; private float b_height = 1.5f; private float b_width = .5f; private float b_length = 1.0f; private float a_height = 1.5f; private float a_width = .5f; private float a_length = .5f; private float l_height = 1.5f; private float l_width = .5f; private float l_length = .5f; private float h_x_s = 0.0f*c; private float h_y_s = 5.0f*c; private float h_z_s = 0.0f*c; private float h_x; private float h_y; private float h_z; private float b_x_s = 0.0f*c; private float b_y_s = 2.5f*c; private float b_z_s = 0.0f*c; private float b_x; private float b_y; private float b_z; private float a_x_s = 1.5f*c; private float a_y_s = 2.5f*c; private float a_z_s = .0f*c; private float a_x; private float a_y; private float a_z; private float l_x_s = .5f*c; private float l_y_s = -.5f*c; private float l_z_s = 0.0f*c; private float l_x; private float l_y; private float l_z; private float a_rotate = 0f; private float a_speed = .6f; private float l_rotate = 0; private float l_speed = .6f; private double a_c; private double l_c; private double a_s; private double l_s; private float max_a = 30f; //angle private float max_l = 30f; //angle private boolean a_forward; private boolean l_forward; private float move_z = 0; //set to 0 private float speed_z = .01f*separation; private float max_z = 2*separation; private boolean forward_z;

P.S. The comments aren't actually what the variables are for, just reminders to me while debugging. Also, my variable names will probably meet much disapproval from professional developers, but it's something I can keep track of easily. And like I said before, the majority of these variables are actively changing, and used in multiple methods for that matter, but a few I'm away I can hide away.


It looks like your variables are screaming for you to put them in classes.

I'm just inferring based on your variable names, but I'm almost certain that's what you need.

For example, you might implement 3 classes like the ones at the bottom of this answer.

This would allow your variable declarations to look something like this:

<strong>Variable declarations:</strong>

Polygon polygonA = new Polygon(1.5, .5, .5); Polygon polygonB = new Polygon(1.5, .5, 1.0); Polygon polygonL = new Polygon(1.5, .5, .5); Coordinate coordH = new Coordinate(); Coordinate coordB = new Coordinate(); Transform transformA = new Transform(0, .6); Transform transformL = new Transform(0, .6);

<strong>Example class definitions:</strong> (each of these goes in a separate file)

class Polygon { float height; float width; float length; public Polygon(int h, int w, int l) { height = h; width = w; length = l; } } class Coordinate { float x; float y; public Coordinate() {} public Coordinate(float xCoord, float yCoord) { x = xCoord; y = yCoord; } } class Transform { float rotate; float speed; public Transform(float r, float s) { rotate = r; speed = s; } }


I'm not sure where you got the idea that it is not done to put variables in separate classes. I don't know the intent of your code, but I guess it has something to do with cubes.

You seem to have four types of variables: h, b, a and l, and they are very similar. You should abstract them into a class. Something like this:

public class Cube { private float height; private float width; private float length; private float x_s; private float y_s; private float z_s; private float x; private float y; private float z; // Add getters, setters, constructors, etc... }

And then you can reduce your current code to:

private Cube h; private Cube b; private Cube a; private Cube l;

Quite neat, right?

And you could further abstract the coordinates and sizes into a Vector3 object:

public class Vector3 { private float x; private float y; private float z; }

Making your Cube:

public class Cube { private Vector3 size; private Vector3 position_s; private Vector3 position; }

Or, if size and position are really different things, you could even make a Point class for the X, Y, Z coordinates, and a Size class for the height, length, width values.

By the way, you really should use meaningfull variable names. Things like h, a, b, l and x_s don't mean anything to me. And if you look at your code after some time, it won't mean anything to you either.


  • Unable to find a version of 'Xamarin.Android.Support.Design' that is compatible with '
  • Apache POI autoSizeColumn() is not working
  • Uniform distribution in a small range to generate precise big probability
  • Eclipse throwing error - “file name references to ”main.html“ that does not exist in web content” 40
  • Chart.js - doughnut show active segment tooltip (on click of external button)
  • stumped by WSGI module import errors
  • how to start and check job history on hadoop 2.5.2
  • Too many variables in Java? [closed]
  • How can a find a face containing a predefined point when i have a planar graph embedded on a plane
  • How to run Jacoco with Java 7 and spring-instrument?
  • Asp.net Mvc site running out of memory
  • Line Graph using Flot is over Lapping
  • Save matplotlib pyplot while using tex as pdf
  • Java.exe exited with code 2 in android app built in VS 2015
  • Pdflatex run crashes when executed from Java (on Windows)
  • Polymer 1.0 iron-ajax call is made but on-response does not fire and the data isn't bound
  • Why is OpenCV Gpu module performing faster than VisionWorks?
  • Could annotation based and xml based configuration be used together in spring 2.5?
  • How do you clear the RecordStore of your midlet in Netbeans?
  • Failure to update Laravel 5.1 to 5.2 (these conflict with your requirements or minimum-stability)
  • Symfony2 doctrine:generate:entities chmod operation not permitted
  • DataBound ListBox and Button inside a ScrollViewer/StackPanel construct, with Button appearing below
  • How do I verify Certificate for KeyUsage
  • cd vs !cd vs %cd in IPython
  • Getting started with Pylint for Jython (jython2.5.1)
  • How to merge column data of the same value and sum its specific data
  • Serving static content with Jetty/Jersey/Guice
  • Fastest way to create a sparse matrix of the form A.T * diag(b) * A + C?
  • Open protected word document with OpenXML
  • Mechanize not being installed by easy_install?
  • Inverse Dict in Python
  • Highest supported JSF version for JBoss 4.0?
  • converting Twilio sms body to a string - encoding error for @ symbol
  • Why do I get wrong results for hmac in Python but not Perl?
  • How to find the closest (x, y) position to (x,y) position in another list?
  • python function to set accuracy of float
  • Easy Way to Get Averages Based on Names in List
  • maven-dependency-plugin ignores outputDirectory configuration
  • object play not found in scala application