non static variable cannot be referenced from static context

non static variable cannot be referenced from static context

public class Add{ int foo=7; int bar=8; public int add(int x,int y){ int z=x+y; System.out.println(z); return z; }//add public static void main(String []s){ Add a1=new Add(); a1.add(7,8); Add a2=new Add(); a2.add(foo,bar); }//main }//class

I wrote the following program. When I compiled this I got the error message saying "non static variable cannot be referenced from static context". How to solve this problem?

View Answers

March 7, 2011 at 1:40 PM

public class Add{
    static int foo=7;
    static int bar=8;
    public int add(int x,int y){
        int z=x+y;
        System.out.println(z);
        return z;
        }
        public static void main(String []s){
            Add a1=new Add(); 
            a1.add(7,8);
            Add a2=new Add();
            a2.add(foo,bar);
        }
}









Related Tutorials/Questions & Answers:
non static variable cannot be referenced from static context
"non static variable cannot be referenced from static context". How to solve...non static variable cannot be referenced from static context  public... public static void main(String []s){ Add a1=new Add(); a1.add(7,8
non static variable cannot be referenced from static context
non static variable cannot be referenced from static context  public class Add{ int foo=7; int bar=8; public int add(int x,int y... public static void main(String []s){ Add a1=new Add(); a1.add(7,8
Advertisements
Static Variable
Static Variable  What is the basic use of introducing static variable type in java?Please explain clearly....   The Static means..., it means that the static variable is shared among all instances of the class
STATIC VARIABLE DECLARATION
STATIC VARIABLE DECLARATION  why cannot the static variable declare inside the method
static variable in php
static variable in php  How can i call a global variable as a static variable
What is the importance of static variable?
What is the importance of static variable?  hi, What is the importance of static variable? thanks
how to inherit static variable
how to inherit static variable   how to inherit static variable or methods in Java?? Please explain the entire process of inheriting the variable. Thanks in advance
Serialize the static variable
Serialize the static variable  hello, Can we serialize the static variable?   hiiADS_TO_REPLACE_1 Yes we can serialize the static variable. if u don't want to serialize, u need to declare the variable as transient
What is the difference between a static and a non-static inner class?
What is the difference between a static and a non-static inner class?   Hi, What is the difference between a static and a non-static inner class? Thanks
Static variable in java
{ static double salary; //static variable int age; //non... of of Employee is="+sv.age); // Accessing non static variable through object... variable as salary and  another one is non-static variable as age , age
Example of a instance variable
variable in a main method. If we try to call the non-static method in the main method then the compiler will prompt you that non-static variable cannot be referenced from a static context. We can call the non-static variable by the instance
static
static  What is the exact need of declaring a variable as static?   A static variable is a variable who's single copy in memory is shared by all objects,so any modifications to the static variable will modify it's value
static
static  what r the main uses of static in java   Hi Friend, The Static means that a certain object/variable is resident in memory... is applied to member variables, it means that the static variable is shared among all
STATIC
is an example: public class StaticTest { static{ System.out.println("Hello from... from static method"); } public static void main(String[] args) { helloMethod...STATIC  WHAT IS STATIC BLOCK?EXPLAIN , AND WHAT IS THE DIFFERENCE
static
redeclare static variable in the inherited class  can we redeclare static variable in the inherited class like this: public class StaticClass1...); **//here i have redeclared the static variable as "25" is it possible,then what
static
from object to anther object,then go for static variables...(According to my...Static Concept  in what are all the situation we may use static...,then use static blocks and without creation of object we need to perform some task
Can we declare a static variable inside a method?
Can we declare a static variable inside a method?   Hi, Can we declare a static variable inside a method? thanks
Static
Static  Can i know the real time example for Static method and final variables with defenition? Thank you
Example of a class variable (static variable)
Example of a class variable (static variable)   ... you can define the static class variable in a class. When a number of objects... To see the use of a static variable first of all create a class StaticVariable
Java static method example
Description: Static variable are used within the static method. Non static variable do not exits in the static method. Static variables are shared by its class instances. Static variable can be used without using the instances
static keyword
a non-static method from it. 3)A static method can be accessed directly... instances of the class. 4)A static variable can be accessed directly by the class name... method can access only static data. It can not access non-static data (instance
static functions
call only other static methods and can not call a non-static method from it. 3... other static methods and can not call a non-static method from it. 3)A static...)It is a variable which belongs to the class and not to object(instance). 2)Static
Accessing non-static members through the main method in Java.
Accessing non-static members through the main method in Java.  As an oop rule, a static method can have access only to static variables and static... in Java has access to non-static members (variables or methods) even though
PHP Static Variable and Methods
PHP Static Methods and Variables: In this tutorial we will study when we should use static methods and variables. Static keyword is used to make only one copy of the variable for the class. All objects of that class will access one copy
Example of static method
an instance of the class. If you try to use a non-static method and variable defined in this class then the compiler will say that non-static variable or method cannot be referenced from a static context. Static method can call only other static
net.n2oapp.context - static-context version 7.23.57 Maven dependency. How to use static-context version 7.23.57 in pom.xml?
net.n2oapp.context  - Version 7.23.57 of static-context Maven dependency? How to use  net.n2oapp.context  - Version 7.23.57 of static-context in pom.xml? How to use static-context version 7.23.57 in pom.xml? Learn to use
net.n2oapp.context - static-context version 7.24.1 Maven dependency. How to use static-context version 7.24.1 in pom.xml?
net.n2oapp.context  - Version 7.24.1 of static-context Maven dependency? How to use  net.n2oapp.context  - Version 7.24.1 of static-context in pom.xml? How to use static-context version 7.24.1 in pom.xml? Learn to use
static keyword in java
method, static variable. A static keyword points the following: static methods. static variable.  static methods:-ADS_TO_REPLACE_1 static method... methods only and we do not call a non static method. In static method we can
Maven Repository/Dependency: net.n2oapp.context | static-context
Maven Repository/Dependency of Group ID net.n2oapp.context and Artifact ID static-context. Latest version of net.n2oapp.context:static-context dependencies. # Version Release Date 1
static methods
static methods  why static methods cannot read or write the instance variables
Maven dependency for net.n2oapp.context - static-context version 7.12.2 is released. Learn to use static-context version 7.12.2 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.2. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.3 is released. Learn to use static-context version 7.12.3 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.3. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.4 is released. Learn to use static-context version 7.12.4 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.4. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.5 is released. Learn to use static-context version 7.12.5 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.5. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.6 is released. Learn to use static-context version 7.12.6 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.6. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.7 is released. Learn to use static-context version 7.12.7 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.7. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.8 is released. Learn to use static-context version 7.12.8 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.8. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.9 is released. Learn to use static-context version 7.12.9 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.9. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.10 is released. Learn to use static-context version 7.12.10 in Maven based Java projects
of static-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.10. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.11 is released. Learn to use static-context version 7.12.11 in Maven based Java projects
of static-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.11. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.12 is released. Learn to use static-context version 7.12.12 in Maven based Java projects
of static-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.12. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.13 is released. Learn to use static-context version 7.12.13 in Maven based Java projects
of static-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.13. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.14 is released. Learn to use static-context version 7.12.14 in Maven based Java projects
of static-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.14. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.15 is released. Learn to use static-context version 7.12.15 in Maven based Java projects
of static-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.15. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.16 is released. Learn to use static-context version 7.12.16 in Maven based Java projects
of static-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.16. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.12.17 is released. Learn to use static-context version 7.12.17 in Maven based Java projects
of static-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.12.17. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.13.2 is released. Learn to use static-context version 7.13.2 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.13.2. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.13.5 is released. Learn to use static-context version 7.13.5 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.13.5. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.13.6 is released. Learn to use static-context version 7.13.6 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.13.6. Developer can use this version ( net.n2oapp.context - static-context
Maven dependency for net.n2oapp.context - static-context version 7.13.7 is released. Learn to use static-context version 7.13.7 in Maven based Java projects
-context released The developers of   net.n2oapp.context - static..., the released version of  net.n2oapp.context - static-context library is 7.13.7. Developer can use this version ( net.n2oapp.context - static-context

Ads