• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

variables outside main

How do i declare variables in java that can be used in whole main class like below.
I dont want to create local variables. To me this should work .

public class Mytest1 {

 
    public  int w; //error
    float q;
     String e;
   
    public static void main(String[] args) {
         

        w=4; //error
        e="gdfgdg"; /error
       
         System.out.println(e);
        System.out.println(w);
        System.out.println(new Date());
     
     
       
    }
0
jagguy
Asked:
jagguy
  • 11
1 Solution
 
for_yanCommented:
>How do i declare variables in java that can be used in whole main class

variables which are declared immedaiately after class declaratio not instide any method will vbbe instance variables and are accessible in any in any method
0
 
for_yanCommented:
but in main() method as staticc method you can use onluy static variables
0
 
for_yanCommented:
in this case you should declae r themn this way:
public class Mytest1 {

 
    public static  int w; //error
  static   float q;
    static  String e;
   
    public static void main(String[] args) {
         

        w=4; //error
        e="gdfgdg"; /error
       
         System.out.println(e);
        System.out.println(w);
        System.out.println(new Date());
     
     
       
    }

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
for_yanCommented:
You should read and very well understand difference between static and non-static instancwe variables and methods in Java
0
 
for_yanCommented:
In particular within a static method (like main()) you cannot use non-statoc instance variables - it will casue ciompilation error
0
 
for_yanCommented:
You should understand that usually class is a template on how to create instances of the class. SO instance fields of the class are properties of the instance - you create new instance wand with it you create a bunch of fileds belonging to the intsance
You create another instance and another set of instance variables.
So you change instance variable within one inatnce and other iinstances are not affected

static are quite diffferent.
static varibale is unique for all instances of the particular class, there fore to address it you refere not to particvular inatnce of the class, but ratehr to the class name
So atstic variable is not specific to any insatnce of the class
Once it is changed - the whole world will see that it changed.
0
 
for_yanCommented:
the same with instance methods vs static methods.
Instance methods are invoked on particualr instance and therefore they can operate with instance variables
static mthods are independent of any particular instance.
therefore they cannot use any instance variables; they can use only static variables
0
 
for_yanCommented:
jsut one remark on your question:

>I dont want to create local variables

This is not wise attitide. those avaribles which are used only iwithin the same method or even sometimes in a small block of code  within some methods do not need to be and should not be declared as either instance, let alone static.
You want to use ilocal variables whenever it is necessary

Only small part of variables which need to be accessed from, different methods should be declared as instance variables. Those which are nececsarry for soem particular calculation only within one method shoudl be decalerd in that methdo and shoudl be local
0
 
for_yanCommented:
This is your original code compiled and working (see outpt be,low)

public class Mytest1 {


    public static int w; 
    float q;
     static String e;

    public static void main(String[] args) {


        w=4; 
        e="gdfgdg";

         System.out.println(e);
        System.out.println(w);
        System.out.println(new java.util.Date());

    }
}

Open in new window


Output:
gdfgdg
4
Thu Mar 15 16:18:37 PDT 2012

Open in new window

0
 
jagguyAuthor Commented:
in the main I see I cant use non-static variables.
it kind of isnt logical for the main but for other classes then ok.
0
 
for_yanCommented:
No, it is logical because main() is a static method - one common method for all instances - therfore you cannot use inastance variables not associated with any particular instance there, but you can use static variables which are not associated with any instance eiether
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now