Solved

variables outside main

Posted on 2012-03-15
12
195 Views
Last Modified: 2012-06-27
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
Comment
Question by:jagguy
  • 11
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37727154
>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
 
LVL 47

Expert Comment

by:for_yan
ID: 37727156
but in main() method as staticc method you can use onluy static variables
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37727162
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 47

Expert Comment

by:for_yan
ID: 37727168
You should read and very well understand difference between static and non-static instancwe variables and methods in Java
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37727173
In particular within a static method (like main()) you cannot use non-statoc instance variables - it will casue ciompilation error
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37727175
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37727182
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37727187
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37727193
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
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37727206
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
 

Author Comment

by:jagguy
ID: 37727348
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37727351
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question