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;
}
}
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;
}
}
I'm trying to call a method from within another method. I'm understanding this simply enough, until one of those methods needs a variable carried through, and then nothing I try works.
I know that I could do this in one method, but my coursework needs me to lay it out in such a way. Why doesn't this work?
public class test2 {
public static void testMethod() {
int randomNumber = 1;
}
public static void anotherTestMethod(int randomNumber) {
System.out.println(randomNumber);
}
public static void main(String[] args) {
anotherTestMethod();
}
}
You are calling a method that has an int parameter in its signature. You should pass that parameter when calling the method. I think you are trying to use a global variable, in that case, you should declare it outside any method, as a part of the class.
public class test2 {
public static int testMethod() {
int randomNumber = 1;
return randomNumber;
}
public static void anotherTestMethod() {
System.out.println(testMethod());
}
public static void main(String[] args) {
anotherTestMethod();
}
}
I have a non static method abc() in the class that contains main() method. Now how to call this method abc() from main. Can i create an instance of the class in the same class?
public class A
{
public static void main(String[] args)
{
//how to call abc() here?
}
private int abc()
{
return 2;
}
}
You have to instanciate current class, use this code:
public static void main(String[] args)
{
A a = new A();
int value = a.abc();
}
public class Test {
public static void main(String[] args) {
System.out.println(Hello.a1);
}
}
class Hello {
static final int a1=10;
static {
System.out.println("SB");
}
}
This code is always printing 10 but not printing SB.Why?
A static final field is implemented as a compile-time constant which is replicated in the accessing method/class without any reference to the context of definition (its class). This is why accessing it does not trigger the static class block.
This is basically due to the final keyword. If you remove final as follows:
public class Test {
public static void main(String[] args) {
System.out.println(Hello.a1);
}
}
class Hello{
static int a1=10;
static{
System.out.println("SB");
}
}
You will see that SB is printed as expected.
You are using static final constant variable. This variable will be replaced at the compile time by the actual constant value, in-order to increase the performance. If you have a look at the compiled binary code (Yes you cannot ;) but technically speaking with an assumption) it will be something similar to this:
public class Test {
public static void main(String[] args) {
System.out.println(10); // Constant
}
}
class Hello {
static final int a1=10;
static {
System.out.println("SB");
}
}
Based on this code, class Hello will not be loaded into the RAM. So it will not print the SB.
Your static code block willbe invoked, when you create an instance of the class. Try this:
public class Test {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(Hello.a1);
}
}
class Hello {
static final int a1 = 10;
static {
System.out.println("SB");
}
}
I have this code:
public class test {
init() {
//How can i get access of args?
}
public static void main(String[] args) {
new test();
}
How can I get access of "args" in init()?
Just pass it as an argument.
public class test{
init(String[] args){
//How can i get access of args?
}
public static void main(String[] args) {
test t = new test();
t.init(args);
}
}
Remember, you should capitalise your class names.
Define an instance field within the class, and create constructor to accept the array and set the field:
public class test{
String[] args;
public test(String[] args){
this.args = args;
}
init(){
}
}
public static void main(String[] args) {
new test(args);
}
Or pass it as an argument to the method.
init(String[] args){
}
If init requires anything within the test class, you must create an instance of the class
test t = new test();
t.init(args);
If init does not require a new instance of test (eg doesn't access any instance variables or methods) you could define init as static and directly call the method:
static init(String[] args){//method declaration
}
public static void main(String[] args) {
test.init(args);//call the method in a static way
}