I am not sure if there is a standard or good practice to create the constant variables in the parent class, and then subclasses reuse.
public class parentA{
protected static final String name = Constants.SYS_NAME;
protected static final String code = Constants.CODE;
}
public class son extends parentA{
private static void main(String[] args)
{
System.out.println("Name: "+name);
System.out.println("Code: "+code );
}
}
A common practice is to use an interface for constants.
interface MyInterface {
public static final int X = 10;
public static final int Y = 20;
}
public class Main {
public static void main(String[] args) {
System.out.println(MyInterface.X);
}
}
Output:
10
Related
In a small example, I'm trying to take information from the String "helloFromMain" in a file called Main.java and move it "outside" the public static void into a public static string in a different file named data.java.
IN THE FILE MAIN.JAVA
public class Main {
public static void main(String[] args){
String helloFromMain = "hello";
}
}
IN THE FILE DATA.JAVA
public class Data {
public static String helloFromData = helloFromMain;
}
Any help would be appreciated.
Also im relatively new to all this
You can set a public static variable from another class.
public class Main {
public static void main(String[] args){
String helloFromMain = "hello";
Data.helloFromData = helloFromMain;
}
}
Also, I've found that it's helpful to set a package for all classes, as it makes it simpler to manage importing and FS structure.
You can do it like this
Simple way.
IN THE FILE MAIN.JAVA
public class Main {
public static String helloFromMain; //declare a public static string, it will be accessible from outside the class.
public static void main(String[] args){
helloFromMain = "hello";
}
}
IN THE FILE DATA.JAVA
public class Data {
public static String helloFromData = Main.helloFromMain;
}
But be aware that Main.helloFromMain will be null until you call the main constructor.
Advance way :
IN THE FILE MAIN.JAVA
public class Main {
private static String helloFromMain; //This time the static variable is private, so you can't directly use it from outside
public static void main(String[] args){
helloFromMain = "hello";
}
//We create a public static method to access the private static variable
public static String getHelloFromMain(){
return helloFromMain;
}
}
IN THE FILE DATA.JAVA
public class Data {
//We call our public static method from Main.
public static String helloFromData = Main.getHelloFromMain();
}
You can create a static method in class Data to set the value of the string. If you do this, you don't need to make the variable in class Data public.
public class Main {
public static void main(String[] args) {
String helloFromMain = "hello";
Data.setHelloFromData(helloFromMain);
}
}
public class Data {
private static String helloFromData;
public static void setHelloFromData(String newValue) {
helloFromData = newValue;
}
}
In my Android application I have to initialize a lot of static Objects before the first Activity starts. From what I know, static variables are initialized when classes are loaded. So, with time the amount of static objects in project began to grow and now I'm getting NullPointerExceptions. In my case static objects may call other static objects in their constructors. So my question is: could some static variables be initialized before variables they depend on and thus cause NullPointersExceptions? Is that possible?
Code example :
private static class HardwareManagersHolder implements HardwareManagers, IManagers {
private final AtomicBoolean senderAcquire = new AtomicBoolean(false);
private final AtomicInteger receiverAcquire = new AtomicInteger(0);
public IAudioManager audioManager;
public IVideoManager videoManager;
public IVibrationManager vibrationManager;
public IBatteryHelper batteryHelper;
#Override
public void configureManager() {
audioManager = AudioHelper.getInstance();
vibrationManager = VibrationManager.getInstance();
videoManager = VideoManager.getInstance();
batteryHelper = BatteryHelper.getInstance();
}
And an Object Holder:
public class VideoManager implements IVideoManager {
private static class VideoManagerHolder {
public static final VideoManager VIDEO_MANAGER_INSTANCE = new VideoManager();
}
public static VideoManager getInstance() {
return VideoManagerHolder.VIDEO_MANAGER_INSTANCE;
}
}
I tried to reconstruct your exception with the snippets you provided. I used the following code:
public interface IVideoManager {}
public class VideoManager implements IVideoManager {
private static class HardwareManagersHolder {
public IVideoManager videoManager;
public void configureManager() {
videoManager = VideoManager.getInstance();
}
}
private static class VideoManagerHolder {
public static final VideoManager VIDEO_MANAGER_INSTANCE = new VideoManager();
}
public static VideoManager getInstance() {
return VideoManagerHolder.VIDEO_MANAGER_INSTANCE;
}
public static void main(String[] arg) {
System.out.println("Start test");
HardwareManagersHolder h = new HardwareManagersHolder();
h.configureManager();
if (h.videoManager == null) {
System.out.println("VideoManager is null");
}
System.out.println("Test finished");
}
}
This code works on my machine. If this code is not working on yours, there is some other fault.
Are you initializing them in a static constructor? They would get called first for precisely this reason.
static
{
VIDEO_MANAGER_INSTANCE = new VideoManager();
}
public class Pname {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a+b=" + (a + b));
System.out.println("Hello World!");
}
}
public class Pname1 {
public static void main(String[] args) {
private String myName = "sha";
private String myHname = "pra";
System.out.println("the adding together is=" + (myName + myHname));
}
}
public class Pname2 {
public static void main(String[] args) {}
}
Create three different java files, Pname.java, Pname1.java and Pname2.java and place your corresponding public classes in these files. In java, one .java source code file can contain at most one public class.
Is it possible to have a "global" variable, i.e., "balance", which all methods can access without parameters?
Something like:
public static void main(String[] args{
makevariablehere
}
Could be called in another method:
public static int someMethod() {
variable = newVariable;
}
You can define it as a static field on the class. See the example below, which stores the number of args passed to the main method in a static field, so that it may be returned by the getNumberOfArgs() method.
public class MyClass {
private static int argCount;
public static void main(String[] args) {
argCount = args.length;
}
public static int getNumberOfArgs() {
return argCount;
}
}
I have two classes that extend the same abstract class. They both need the same constant, but with different values. How can I do this? Some example code to show what I want to do.
abstract class A {
public static int CONST;
}
public class B extends A {
public static int CONST = 1;
}
public class C extends A {
public static int CONST = 2;
}
public static void main(String[] args){
A a = new B();
System.out.println(a.CONST); // should print 1
}
The above code does not compile because CONST is not initialized int class A. How can I make it work? The value of CONST should be 1 for all instances of B, and 2 for all instances of C, and 1 or 2 for all instances of A. Is there any way to use statics for this?
You can't do that.
You can do this, however:
abstract class A {
public abstract int getConst();
}
public class B extends A {
#Override
public int getConst() { return 1; }
}
public class C extends A {
#Override
public int getConst() { return 2; }
}
public static void main(String[] args){
A a = new B();
System.out.println(a.getConst());
}
If a constant has a variable value, it's not a constant anymore. Static fields and methods are not polymorphic. You need to use a public method to do what you want.