Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Blockquote The main reason I didn't want to pass x,y each time is because I have several functions in several SubClasses that each use (x,y, z, etc) and it didnt seem right to pass in like 6-8 variables each time I call a function. The sub classes used to be dependent, but I'm trying to take them out of the file so they can be used in a different project. Blockquote</p> </blockquote> <p>If this is the case then you are better off abstracting your variables out to a container class. If every instance of these no-longer-dependent classes is going to use a large subset of these variables then it makes sense to have them all in one instance. Variables that are logically related should be found in the same place. Your approach should be something like:</p> <pre><code>public class Top_Level_Class(){ Container container = new Container(x, y, p, q, r, s); SubClass1a a = new SubClass1a(container); SubClass1a b = new SubClass1b(container); // gets user input which changes x, y using container's getters and setters public void main(){ // compute and set p, q, r, s a.doA(); b.doB(); } public class SubClass1a(Container c) { // in its own separate file public void doA(c.getX, c.getY, c.getP, c.getQ, c.getR){ // do something that requires x, y, p, q, and r } } public class SubClass1b(Container c) { // in its own separate file public void doB(c.getX, c.getY, c.getQ, c.getR, c.getS){ // does something else that requires x, y, q, r, and s } } public class Container(x, y, p, q, r, s) { //getters and setters for all variables } </code></pre> <p>This keeps you from getting into a variable passing spaghetti-code situation, and when you want to use just one or two of these classes later your code is modular enough to port with just those classes and your container. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload