Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 725355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:19:40+00:00 2026-05-14T06:19:40+00:00

I want less methods. I want a common global TestClass from which I could

  • 0

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);
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:19:40+00:00Added an answer on May 14, 2026 at 6:19 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way, in PHP, to call methods from a parent class using
i want to use the listview flicker"less" control found here Link directly in my
I have some problems when I want to use implicit methods to convert a
Can anyone provide more-or-less real-world examples of when you might want to actually use
I want to create a less than or equal to 10 character unique string
The scenario is I want to get the users who has less than 2
We have Xserver-less CentOS system for continuous integration. So no UI. I want to
want to know why String behaves like value type while using ==. String s1
want to have a Hyperlink-Button in a gridView in which I can display a
There are two methods GetUserAssignedSystems() and GetUserAssignedSystems(string Id) These methods act very differently from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.