Solved

variables outside main

Posted on 2012-03-15
12
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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