views:

201

answers:

7

I want less methods. I want a common global TestClass from which I could use any of its value inside the class.

import java.util.*;
import java.io.*;

public class TestClass {
        TestClass(String hello){
                String hallo = hello;
                String halloSecond = "Saluto!";
        }
        public static void main(String[] args) {
                TestClass test = new TestClass("Tjena!");
                System.out.println("I want "Tjena!": " + test.hallo);
                TestClass testSecond = new TestClass("1");
                System.out.println("I want Saluto!:" + test.halloSecond);
                System.out.println("I want Saluto!:" + testSecond.halloSecond);

                // How can I get glob.vars like the "Saluto!"?
        }
}

[Clarification Needed] I cannot understand the no-use of GLOB.VARS. Please, see the code belowe where you cannot access the GLOB.VARS without an instance, hence the error. If I quarantee no malicious code can make an instance, is there any problem in using GLOB.vars?

$ javac TestClass.java 
TestClass.java:19: non-static variable hallo cannot be referenced from a static context
  System.out.println("It did not get to the GLOB.VAR: " + hallo);
                                             ^
1 error
$ cat TestClass.java 
import java.util.*;
import java.io.*;

public class TestClass {
        public String hallo;
        public String halloSecond;

        TestClass(String hello){
                hallo = hello;
                halloSecond = "Saluto!";
        }
        public static void main(String[] args) {
                TestClass test = new TestClass("Tjena!");
      System.out.println("It did not get to the GLOB.VAR" + hallo);
        }
}
+8  A: 

I want less methods.

You shouldn't. Methods should not be measured by count or size. They should exist if they have a separate responsibility.

I want a common global TestClass from which I could use any of its value inside the class.

This doesn't make much sense. I guess you need instance variables

 private String hello;
 private String helloSecond;

 TestClass(String hello){
        hallo = hello;
        halloSecond = "Saluto!";
 }

 public String getHello() { return hello; }
 public String getHelloSecond() { return helloSecond; }

How to have global values inside a class?

global variables can be achieved by defining them static:

public static String var;

But using these is a very bad practice. You must not use them.

Bozho
Yes, yes there is a problem.
Kaleb Brasee
@HH I added some explanation about global variables to my answer, hope it help clarify the issue.
Péter Török
+2  A: 
import java.util.*;
import java.io.*;

public class TestClass {
        public String hallo;
        public String halloSecond;

        TestClass(String hello){
                hallo = hello;
                halloSecond = "Saluto!";
        }
        public static void main(String[] args) {
                TestClass test = new TestClass("Tjena!");
                System.out.println("I want "Tjena!": " + test.hallo);
                TestClass testSecond = new TestClass("1");
                System.out.println("I want Saluto!:" + test.halloSecond);
                System.out.println("I want Saluto!:" + testSecond.halloSecond);

                // How can I get glob.vars like the "Saluto!"?
        }
}
oedo
+5  A: 

Global variables are generally considered a Very Bad Idea. Yes, it can be difficult to design your classes so that every class contains all (or most) of the variables it needs to do its work. But it's much more difficult to deal with code that's based on global variables (Mainly because it becomes very hard to predict what effects changes in one part of the code will have on the rest of it).

Michael Borgwardt
I also find global constants difficult to manage. I like to keep the constants in the classes they're used in, though some may go in the main class because they apply to the application. Java's core libraries are also designed like this. Some people create a Constants class and stuff all the constants in there, and I waste my time trying to sort through and find the one I want.
Marcus Adams
@Marcus: What's even worse: such a "Constants" class ends up being used nearly everywhere, any change in it requires a rebuild of all classes that use it - and it tends to get changed frequently. When rebuilding the entire project takes 15 minutes, you really start feeling the pain...
Michael Borgwardt
+4  A: 

I guess you are looking for something like this:

public class TestClass {
    public final String hallo;
    public static final String halloSecond = "Saluto!";

    TestClass(String hello){
        String hallo = hello;
    }

    public static void main(String[] args) {
        TestClass test = new TestClass("Tjena!");
        System.out.println("I want "Tjena!": " + test.hallo);
        TestClass testSecond = new TestClass("1");
        System.out.println("I want Saluto!:" + test.halloSecond);
        System.out.println("I want Saluto!:" + testSecond.halloSecond);
    }
}

The value of hallo is set in each instance of TestClass. The value of halloSecond is a constant, shared by all instances of the class and visible for the whole app. Note that with this code your IDE/compiler probably gives you a warning upon test.halloSecond - it should be qualified by the class name, like TestClass.halloSecond, rather than an instance name.

Update on global variables: the main problem with global variables is that they make the code

  • harder to understand - if a method uses global variables, you can't see simply from its signature what data is it actually manipulating
  • harder to test - same method is difficult to test isolated in unit tests, as you have to (re)set all global variables it depends on to the desired state before each unit test
  • harder to maintain - global variables create dependencies, which easily make the code into a tangled mess where everything depends on everything else

In Java everything is inside a class, so you can't have "classic" global variables like in C/C++. However, a public static data member is still in fact a global variable.

Note that the code sample above, halloSecond is a global constant, not a variable (as it is declared final and is immutable), which alleviates much of these problems (except maybe the dependency issue).

Péter Török
A: 

Actually you can define static members in a class, and their values will be shared across all instances, and even without a instance. You may also check the Singleton pattern.

However, I don't encourage novice programmers to take this approach. It's better to study the OOP itself, and take advantage of its encapsulation. After all, that's what was made for!

jweyrich
A: 

The problem in your "clarification" (which seems to be a completely separate question) is that hallo is an instance variable which you are trying to access from static method. Static content does not belong to any instance and it would be impossible to know which instance to access. To fix that particular peace of code, declare the variables static also.

fish
+1  A: 

The important thing to note is that instance variable declarations go inside the class declaration, but outside of the constructor or any method. I always put them at the top.

import java.util.*;
import java.io.*;

public class TestClass {

        // Instance variables
        public String hallo;

        TestClass(String hello){
          hallo = hello;
        }

        public static void main(String[] args) {
                TestClass test = new TestClass("Tjena!");
                System.out.println("I want Tjena!:" + test.hallo);
                TestClass testSecond = new TestClass("Saluto");
                System.out.println("I want Saluto!:" + testSecond.hallo);
        }
}

Your example doesn't really suggest the use of "global" or class variables. Simply add the word static. Also, you then prefix the variable with the class name, not an instance name when referencing it:

import java.util.*;
import java.io.*;

public class TestClass {

        // Class variables
        public static String hallo;

        TestClass(String hello){
          hallo = hello;
        }

        public static void main(String[] args) {
                TestClass test = new TestClass("Tjena!");
                // Reference class variable with "TestClass", not "test"
                System.out.println("I want Tjena!:" + TestClass.hallo);
                TestClass testSecond = new TestClass("Saluto");
                System.out.println("I want Saluto!:" + TestClass.hallo);
        }
}

To use "constants". Simply add "final" to the declaration. This means the variables won't change once they're declared:

import java.util.*;
import java.io.*;

public class TestClass {

        // Class "constants"
        public static final String hallo = "hello";
        public static final String halloSecond = "Saluto!";

        TestClass(){
        }

        public static void main(String[] args) {
                TestClass test = new TestClass();
                // Reference class variable with "TestClass", not "test"
                System.out.println("I want Tjena!:" + TestClass.hallo);
                TestClass testSecond = new TestClass();
                System.out.println("I want Saluto!:" + TestClass.halloSecond);
        }
}
Marcus Adams
What is the implicit difference in data-acquisation btw private-instance-vars-with-their-getters and static-class-vars? Their implicit meaning is at least the same: to get information restrictly.
HH
@HH, all three examples use public variables. The difference is that with a static variable, you only have one global value for the class. With an instance variable, you have one value per object (instance). You could make them private and use getters and setters for any of them. However, for the static variables, you'd probably want to make the getter and setter methods static. Notice that the usage of the variables change for each example I gave, not just the declaration.
Marcus Adams