Can't access method from other class

Cannot be referenced from a static context

Page 1 of 1

10 Replies - 4542 Views - Last Post: 03 December 2010 - 09:35 PM Rate Topic: -----

#1 PietO88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 10

Can't access method from other class

Posted 02 December 2010 - 06:10 AM

Hello everyone.

I just subscribed to this forum as numerous threads on here have already helped me with my simple Java programs.
I am a Technical Computing student and have recently started to learn about and program in the Java language.
For my project I need to make a Java GUI with Netbeans which will interact with a MySQL database using JDBC.

So far so good, but I want to make a separate class CheckLogin.java which will check if the login information is the same as in the database.
I want to make the constructor of this class set a text in a textfield of the main login form (Login.java) on error.

Something like statusTextField.setText("Login not correct."). Since this textfield is declared private in the source code and I cannot change this in the Netbeans builder, I thought of writing a method in the Login.java which would do this for me.

Method in Login.java:
    public static void setStatus(String status) {
        statusTextField.setText(status);
    }


Now, when I try to use the
Login.setStatus("Login not correct")
of the CheckLogin.class I get an error in the Login.java class saying:

"non-static variable statusTextField cannot be referenced from a static context"

If I remove the static from the setStatus method I get the same error in the CheckLogin.java about setStatus(lang.java.String).
I have searched pages and pages of google but I cannot find the solution for this or figure out where this is coming from with my limited programming experience. I hope someone can point me in the right direction.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Can't access method from other class

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Can't access method from other class

Posted 02 December 2010 - 06:17 AM

static members are class members. they are different from instance members by that they are always the same for each instance.
however, instance members (non static) can be different in each instance.
so, when you declare a static method, you can't refer to a non static (instance member in it).
i.e. you must declare statusTextField as static in order to use it in the static method.
Was This Post Helpful? 0
  • +
  • -

#3 PietO88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 10

Re: Can't access method from other class

Posted 02 December 2010 - 06:23 AM

Thanks for your quick reply.
The code of the components is not editable in my Netbeans class code. So if I cannot declare it as static, is there no way to reference to the setStatus method from outside its class?
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Can't access method from other class

Posted 02 December 2010 - 06:27 AM

The problem is not referring the method outside the class. the problem is that you cannot use non-static members inside a static method.
so that code is wrong:
public static void setStatus(String status) {
    statusTextField.setText(status);
}

unless statusTextField is declared as static.
Or, unless you declare setStatus as non-static method.
i.e:
public void setStatus(String status) {
    statusTextField.setText(status);
}

Was This Post Helpful? 0
  • +
  • -

#5 PietO88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 10

Re: Can't access method from other class

Posted 02 December 2010 - 06:37 AM

Ah I see. The only reason I tried declaring the setStatus method as static is because if I don't, I get the same error in the CheckLogin.java

Here is my CheckLogin.java:

import java.sql.*;
/**
 *
 * @author Piet
 */
public class CheckLogin {
    CheckLogin(String userName, String password) {
        try {
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
            String url = "";

            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection(url, userName, password);
            //Unfinished code
        } catch (Exception e) {
            System.out.println(e);
            Login.setStatus("Login not correct.");
        }
    }
}


And the method in Login.java:
    public void setStatus(String status) {
        statusTextField.setText(status);
    }


As soon as I remove the static in the setStatus method I get this error in the CheckLogin.java:


"non-static method setStatus(java.lang.String) cannot be referenced from a static context"

I have never had to use static for anything so far so I have no knowledge about where this error is coming from.
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Can't access method from other class

Posted 02 December 2010 - 07:28 AM

ok, so Login is a class you declared.
in order to call an instance method you have to create an Object of the class and then call the method, like:
Login l = new Login(/*parameters*/);
l.setStatus("status");

The way you call setStatus is as a static method.
Was This Post Helpful? 0
  • +
  • -

#7 PietO88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 10

Re: Can't access method from other class

Posted 02 December 2010 - 07:55 AM

Ah ok I see your point now.
But my Login.java is my main method which creates a login form. The login form should then check the input using
CheckLogin connect = new CheckLogin(fieldUsername, pwHash);


So since this connect instance is created by the Login.java, I can't really create a new instance of that since it should return the outcome to the Login.java
I'm not very good at explaining but I hope you get the general idea. I guess I don't really understand the hierarchy.
Was This Post Helpful? 0
  • +
  • -

#8 PietO88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 10

Re: Can't access method from other class

Posted 03 December 2010 - 08:20 AM

Anyone?
Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Can't access method from other class

Posted 03 December 2010 - 09:25 AM

Why do you declare methods as static in the first place?
Do you know what are static members and when to use them?
here is sun's tutorial
if you find yourself using instance members inside static methods, perhaps those methods should'nt be declared as static.
just remove the static declaration from the method and you'll do fine.

when you say Login, you mean CheckLogin?
Was This Post Helpful? 0
  • +
  • -

#10 PietO88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 10

Re: Can't access method from other class

Posted 03 December 2010 - 09:41 AM

Thanks for the link. I have basic understanding about static members but I am not trying to make anything static. It's just that I create a new instance of a class (eg. in ActionPerformed) to check a condition.

I want this class (CheckLogin) to set the statusField (JTextField) of the main class (Login). But as soon as I try to access this JTextField it gives me this repeated error.

So I tried to make a public method (setStatus) in Login to make that set the status field to a particular text using the String paramater. But as soon as I use Login.setStatus("") it gives me the same error making it uncompilable.

Maybe it's due to me not understanding anything about swing components etc. I just have to make a GUI using the drag and drop features of the Netbeans pallette and I create functionality by adding code in the actionperformed of buttons. The GUI builder created uneditable code so I can't change any features of any components. I have no idea why I get this static error while I have never used static anywhere in my code.

Hope I clarified the problem and situation a bit.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Can't access method from other class

Posted 03 December 2010 - 09:35 PM

Actually you are a little cheating wit OO design
Your CheckLogin constructor performs 2 actions:
- create the object
- run the task it was created for

I can cope with that

but as an instance creation can't return a status you will have to do it that way to be more by the OO book

CheckLogin connect = new CheckLogin(fieldUsername, pwHash);
if(connect.isStatusOK())
continue with processing

so your CheckLogin class will have to set up a flag in its instance variables saying if the login was correct or not
and than have a isStatusOK() method that retreives the status of that connection

Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1