Solved

variables outside main

Posted on 2012-03-15
12
194 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
recursion example 16 124
Tomcat: Unable to run tomcat service. 2 18
printf performancy 11 32
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 …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

806 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