Results 1 to 17 of 17
  1. #1
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default Static variable context Errors ?

    I m getting errors like this -->

    1. Non-static variable cannot be referenced from a static context.

    2. Static viariable cannot be referenced from non-static context.


    Can any body clear my Concept about that.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Could you write a 10 line program that shows these errors and we'll explain.

  3. #3
    karthigr is offline Member
    Join Date
    Aug 2008
    Posts
    1
    Rep Power
    0

    Default

    Hi

    can any one help me how to load CSv file to table using java .

  4. #4
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Could you write a 10 line program that shows these errors and we'll explain.
    Norm i will ask about the 1st problem later .. pls give a look to this one -->


    class exshyone {
    int a;
    exshyone() {
    a=10;
    System.out.println(a);
    }
    }

    class shyone extends exshyone {

    a=20;
    System.out.println(a);
    public static void main(String []ar) {
    shyone one =new shyone();
    }
    }



    Errors-->

    D:\jdk1.5.0_06\bin\shyone.java:11: <identifier> expected
    a=20;
    ^
    D:\jdk1.5.0_06\bin\shyone.java:12: <identifier> expected
    System.out.println(a);
    ^
    2 errors



    Questions -->

    1. Can't I access variable 'a' of parent in child like the above prog.

    2. why we require the construct to declare & initialize the variable in parent.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Program statements like a=20; must be inside a method or constructor.
    why we require
    The compiler isn't real smart and easily gets confused if you don't follow the rules.

  6. #6
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    but if i will put int 'a' in a constructor or function it will become local for that only. that means i cannot access that class level variable in the child class.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    No, it means that you have hidden the 'a' in the Constructor. The parent's 'a' is still visible to other methods. Modify your above program to see.

  8. #8
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default wats the probl. in previous code ?

    Norm, i m able to access the variable declared in parent class in child one. here is the code-->


    class Parent
    {
    int d=100;
    void f1()
    {
    System.out.println("IN PARENT f1");

    }

    }

    class Child1 extends Parent
    {
    void f2()
    {
    System.out.println("value of D"+d); // Here i m accessing the variable D of Class levl. var. of Parent
    System.out.println("in Child1 f2");
    // Child1 ob1=new Child2();
    // ob1.f1();

    }
    }

    /*
    void f1() {
    System.out.println("in Child1 f1");
    }
    }

    */


    public class Child2 extends Child1
    {

    /*
    void f2() {

    System.out.println("In Child2 f2");
    Child2 ob1=new Child2();
    ob1.f1();
    }
    void f1() {
    System.out.println("In Child2 f1");
    }

    */

    public static void main(String[] args)
    {
    Parent p=new Parent();
    p.f1();

    Child1 ob=new Child1();
    ob.f2();

    /* Child1 ch1=new Child1();
    ch1.f2(); */
    }
    }



    then why i m not able to do that in the previous code... reasons

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    i m not able to do that in the previous code
    Can you be more specific?
    what does do that mean? What variables in what methods/classes?

  10. #10
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    No, it means that you have hidden the 'a' in the Constructor. The parent's 'a' is still visible to other methods. Modify your above program to see.
    Could u pls modify the code 1 for me ..i tried ..didn't work.

  11. #11
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default wats the probl. in previous code ?

    Norm, i m able to access the variable declared in parent class in child one. here is the code-->


    class Parent
    {
    int d=100;
    void f1()
    {
    System.out.println("IN PARENT f1");

    }

    }

    class Child1 extends Parent
    {
    void f2()
    {
    System.out.println("value of D"+d); // Here i m accessing the variable D of Class levl. var. of Parent
    System.out.println("in Child1 f2");
    // Child1 ob1=new Child2();
    // ob1.f1();

    }
    }

    /*
    void f1() {
    System.out.println("in Child1 f1");
    }
    }

    */


    public class Child2 extends Child1
    {

    /*
    void f2() {

    System.out.println("In Child2 f2");
    Child2 ob1=new Child2();
    ob1.f1();
    }
    void f1() {
    System.out.println("In Child2 f1");
    }

    */

    public static void main(String[] args)
    {
    Parent p=new Parent();
    p.f1();

    Child1 ob=new Child1();
    ob.f2();

    /* Child1 ch1=new Child1();
    ch1.f2(); */
    }
    }



    then why i m not able to do that in the previous code... reasons

  12. #12
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Can you be more specific?
    what does do that mean? What variables in what methods/classes?
    I hav tried the code 1 -->


    class exshyone {
    int a=100;
    exshyone() {
    a=10;
    System.out.println(a);
    }
    }


    class shyone extends exshyone {

    void show() {

    a=290;
    System.out.println(a);
    }

    public static void main(String []ar) {

    shyone one =new shyone();
    one.show();

    }
    }

    i m able to access var. ' a ' When i put

    a=290;
    System.out.println(a);

    in any Function or in Cunstructor .

    cant we execute the code like this -->


    class exshyone {
    int a=100;
    exshyone() {
    a=10;
    System.out.println(a);
    }
    }


    class shyone extends exshyone {

    //void show() {

    a=290;
    System.out.println(a);

    // }

    public static void main(String []ar) {

    shyone one =new shyone();
    one.show();

    }
    }


    if not wat is the reason java dont permit this code.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    You don't say what the problem is?
    java dont permit this code.
    If you get errors, please copy them here.

  14. #14
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default wats the probl. in previous code ?

    Norm, i m able to access the variable declared in parent class in child one. here is the code-->


    class Parent
    {
    int d=100;
    void f1()
    {
    System.out.println("IN PARENT f1");

    }

    }

    class Child1 extends Parent
    {
    void f2()
    {
    System.out.println("value of D"+d); // Here i m accessing the variable D of Class levl. var. of Parent
    System.out.println("in Child1 f2");
    // Child1 ob1=new Child2();
    // ob1.f1();

    }
    }

    /*
    void f1() {
    System.out.println("in Child1 f1");
    }
    }

    */


    public class Child2 extends Child1
    {

    /*
    void f2() {

    System.out.println("In Child2 f2");
    Child2 ob1=new Child2();
    ob1.f1();
    }
    void f1() {
    System.out.println("In Child2 f1");
    }

    */

    public static void main(String[] args)
    {
    Parent p=new Parent();
    p.f1();

    Child1 ob=new Child1();
    ob.f2();

    /* Child1 ch1=new Child1();
    ch1.f2(); */
    }
    }



    then why i m not able to do that in the previous code... reasons

  15. #15
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default wats the probl. in previous code ?

    Norm, i m able to access the variable declared in parent class in child one. here is the code-->


    class Parent
    {
    int d=100;
    void f1()
    {
    System.out.println("IN PARENT f1");

    }

    }

    class Child1 extends Parent
    {
    void f2()
    {
    System.out.println("value of D"+d); // Here i m accessing the variable D of Class levl. var. of Parent
    System.out.println("in Child1 f2");
    // Child1 ob1=new Child2();
    // ob1.f1();

    }
    }

    /*
    void f1() {
    System.out.println("in Child1 f1");
    }
    }

    */


    public class Child2 extends Child1
    {

    /*
    void f2() {

    System.out.println("In Child2 f2");
    Child2 ob1=new Child2();
    ob1.f1();
    }
    void f1() {
    System.out.println("In Child2 f1");
    }

    */

    public static void main(String[] args)
    {
    Parent p=new Parent();
    p.f1();

    Child1 ob=new Child1();
    ob.f2();

    /* Child1 ch1=new Child1();
    ch1.f2(); */
    }
    }



    then why i m not able to do that in the previous code... reasons

  16. #16
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default

    if i write like this (without function or Cunstructor ) -->


    class exshyone {
    int a=100;
    exshyone() {
    a=10;
    System.out.println(a);
    }
    }


    class shyone extends exshyone {

    //void show() {

    a=290; // Here (like this)
    System.out.println(a);

    // }

    public static void main(String []ar) {

    shyone one =new shyone();
    one.show();

    }
    }


    Errors-->


    D:\jdk1.5.0_06\bin\shyone.java:15: <identifier> expected
    a=290;
    ^
    D:\jdk1.5.0_06\bin\shyone.java:16: <identifier> expected
    System.out.println(a);
    ^
    2 errors

  17. #17
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default wats the probl. in previous code ?

    Norm, i m able to access the variable declared in parent class in child one. here is the code-->


    class Parent
    {
    int d=100;
    void f1()
    {
    System.out.println("IN PARENT f1");

    }

    }

    class Child1 extends Parent
    {
    void f2()
    {
    System.out.println("value of D"+d); // Here i m accessing the variable D of Class levl. var. of Parent
    System.out.println("in Child1 f2");
    // Child1 ob1=new Child2();
    // ob1.f1();

    }
    }

    /*
    void f1() {
    System.out.println("in Child1 f1");
    }
    }

    */


    public class Child2 extends Child1
    {

    /*
    void f2() {

    System.out.println("In Child2 f2");
    Child2 ob1=new Child2();
    ob1.f1();
    }
    void f1() {
    System.out.println("In Child2 f1");
    }

    */

    public static void main(String[] args)
    {
    Parent p=new Parent();
    p.f1();

    Child1 ob=new Child1();
    ob.f2();

    /* Child1 ch1=new Child1();
    ch1.f2(); */
    }
    }



    then why i m not able to do that in the previous code... reasons

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 01:35 AM
  2. About static variable
    By MarkWilson in forum New To Java
    Replies: 5
    Last Post: 06-27-2008, 02:43 PM
  3. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 09:51 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •