DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2003
    Posts
    2

    java newbie. some error

    hi! this is my code. i am getting the error given in the end. can any one help.
    (i have just written the matrix multiplicartion. dont know if it works.)


    class Mult
    {
    private int arr1[][] ={{1,2,3},{2,3,4},{4,5,6}};
    private int arr2[][] ={{1,2,3},{2,3,4},{4,5,6}};

    public static void main(String[] args)
    {
    System.out.println("Hello World!");
    Matrix m = new Matrix();

    m.PrintMatrix(arr1);

    }
    }

    class Matrix
    {
    int Mat [][]=new int[3][3];
    int tmp=0;
    void Multiply(int arr1[][],int arr2[][])
    {
    for (int k=0;k<3 ;k++ )
    {

    for(int i=0;i<3;i++)
    {
    for (int j=0;j<3 ;j++ )
    {
    tmp+=arr1[k]*arr2[k][j];
    }
    Mat[k]=tmp;
    tmp=0;

    }
    }
    }
    void PrintMatrix(int arr[][])
    {

    System.out.println("");
    for (int i=0;i<3 ;i++ )
    {

    for(int j=0;j<3;j++)
    System.out.print(arr[j]+ " ");
    System.out.println("");
    }

    }


    }
    ///////////////////////////////////////////////////
    Mult.java:11: non-static variable arr1 cannot be referenced from a static contex
    t
    m.PrintMatrix(arr1);
    ^
    1 error
    ////////////////////////////////////////////////////

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    The compiler message says it all: you're trying to use non-static variables (i.e. those which are attached a particular instance of that class rather than the class itself) from a static portion of code (a portion of code which is associated with a class, not an instance of that class).

    Solution: Declare the variables thus:
    Code:
    private static int arr1[][] ={{1,2,3},{2,3,4},{4,5,6}};
    private static int arr2[][] ={{1,2,3},{2,3,4},{4,5,6}};
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Jul 2003
    Posts
    2
    hi!
    kindly note that i am new to java;

    in the abouve progrtam i pass non static members of a class to nonstatic member functin of another class. so why the problem.


    Originally posted by ArchAngel
    The compiler message says it all: you're trying to use non-static variables (i.e. those which are attached a particular instance of that class rather than the class itself) from a static portion of code (a portion of code which is associated with a class, not an instance of that class).

    Solution: Declare the variables thus:
    Code:
    private static int arr1[][] ={{1,2,3},{2,3,4},{4,5,6}};
    private static int arr2[][] ={{1,2,3},{2,3,4},{4,5,6}};

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    arr1 and arr2 are non-static instance variables of the Mult class.

    In the *static* method of the Mult class, there is some code (the code itself is irrelevant) which makes reference to the *non-static* instance variable arr1.

    --

    OK. The important point here is that it doesn't matter if you're passing the variables to an object instance. The important point is that you're trying to pass those *non-static* variables from a *static* context (the main method).
    ArchAngel.
    O:-)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links