Solved

variables outside main

Posted on 2012-03-15
12
190 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now