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
}
Related
i'm new with java and i have some a difficult to access some variables from different methods but in the same class.
my code:
class upgradeSim {
public static void main(String[] args) throws JSchException {
System.out.print("\n[INFO]: Please enter the version you would like to upgarde to: ");
Scanner inputVer = new Scanner(System.in);
String uiVersion = inputVer.nextLine();
}
public static void SendShhCmd() {
//some code
}
public static void StartUpgrade() throws JSchException {
String cmd = ("SCP data/mdusr/perforce/automationtools/builds/ui/"+uiVersion);
}
}
the problem is that StartUpgrade method does not recognized uiVersion variable.
i tired using "this" and "super" but no luck...
thanks for the help.
Declare the uiVersion as a static member of the upgradeSim class:
class upgradeSim {
public static String uiVersion;
public static void main(String[] args) throws JSchException {
System.out.print("\n[INFO]: Please enter the version you would like to upgarde to: ");
Scanner inputVer = new Scanner(System.in);
uiVersion = inputVer.nextLine();
}
...
public static void StartUpgrade() throws JSchException {
// Now, 'uiVersion' is accessible
String cmd = ("SCP data/mdusr/perforce/automationtools/builds/ui/"+uiVersion);
}
}
Make uiVersion a class variable:
class upgradeSim {
public static String uiVersion;
etc.
}
And set the variable in your main method like this:
uiVersion = inputVer.nextLine();
You should create a global variable named uiVersion like this:
class upgradeSim {
static String uiVersion ;
public static void main(String[] args) throws JSchException {
...
uiVersion = inputVer.nextLine();
...
uiVersion varibale is local, only accessible in main method, so you cannot access to it from StartUpgrade method.
Declare the uiVersion as a static member of the UpgradeSim class
class UpgradeSim {
private static String UI_VERSION;
public static void main(String[] args) throws JSchException {
System.out.print("\n[INFO]: Please enter the version you would like to upgarde to: ");
Scanner inputVer = new Scanner(System.in);
UI_VERSION = inputVer.nextLine();
}
public static void SendShhCmd() {
//some code
}
public static void StartUpgrade() throws JSchException {
String cmd = ("SCP data/mdusr/perforce/automationtools/builds/ui/"+uiVersion);
// use UI_VERSION;
}
}
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");
}
}
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'm confused between when to use public static void xxx() and public void xxx(). The error I'm getting is in the main class.
generateRandomNumber();
The error : (error: non-static method generateRandomNumber() cannot be
referenced from a static context)
getUserInput();
he error : (error: non-static method getUserInput() cannot be
referenced from a static context)
getResult();
The error : (error: non-static method getResult() cannot be referenced
from a static context)
public class HighLowGame {
int randomNumber;
int guess;
public void generateRandomNumber(){
randomNumber = (0+(int)(Math.random() * ((0 - 99) + 1)));
}
public void getUserInput(){
guess = Integer.parseInt(JOptionPane.showInputDialog(
null,
"Plaese input"));
}
public String getResult(){
if(randomNumber<guess){
return "Your number is bigger than magic number";
}
else if (randomNumber>guess) {
return "Your number is smaller than magic number";
}
else {
return "You are correct! "+randomNumber;
}
}
public static void main(String[] args){
generateRandomNumber();
getUserInput();
getResult();
}
}
you cannot access a non-static instance method/variable from static context directly. you will need an instance of your class to access them .
public static void main(String[] args){
HighLowGame ref = new HighLowGame ();
ref. generateRandomNumber();
ref.getUserInput();
ref.getResult();
}
or you can make all these methods static and access them directly. make methods static when you think all the instances of that class should share the date/methods.
You're attempting to call a method which needs an instance of HighLowGame to work.
Change your main method to:-
public static void main(String[] args){
HighLowGame game = new HighLowGame();
game.generateRandomNumber();
game.getUserInput();
game.getResult();
}
You have to call the Methods on an instance of HighLowGame.
public static void main(String[] args){
HighLowGame game = new HighLowGame();
game.generateRandomNumber();
game.getUserInput();
game.getResult();
}
or declare your Methods static like
public static void generateRandomNumber(){ ..}