0

I'm new to java. I'm facing a class declaration because my main is a static class. If i remove the static it will return there's no main class. So all my variable passing in can't be executed, i've tried google it and i've been getting no luck. That's why I'm posting here . Thanks

package javaapplication2;

import java.security.MessageDigest;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JavaApplication2 {

    private void outluck() {

    }
public interface DbType {

    public void mySQL();

    public void microsoftSQLServer();

}

 public static void main(String[] args) {
   outluck();  <--- Error

    }

public String Encrypt(String s) {

}

public String hehe(String ooo){

String x="";
     try {
//
}
 } catch (SQLException ex) {
    }
       return x;
}

public class Database implements DbType {
public void 1()
public void 2()
public void 3()
public void 4()

}
}

The error was :

non-static method outluck() cannot be referenced from a static content.

I tried making it non-static but i still have error. The database class is my class for the database connection.

The hehe class is for do a query towards a db then fetch the data and pass it to encrypt function to update the database.

Jens
  • 67,715
  • 15
  • 98
  • 113
Jeev
  • 3
  • 2
  • @KevinEsche , but it's clashing with my main function. I've even checked that post too. – Jeev Aug 19 '16 at 07:40
  • yeah but your question basicly says "i have no clue what´s the meaning of `static`, so why doesn´t it work if i throw around `static` on everyting generating a compilation error for not beeing `static`". – SomeJavaGuy Aug 19 '16 at 07:41
  • When you get an error, google the exact error you get, and there's a very big chance you'll have your answer. And when you post an error, post the exact, copied error. Don't re-type it with typos. – JB Nizet Aug 19 '16 at 07:42
  • Additionally, *please* try to post code in a readable format. Assuming you're using an IDE, almost all IDEs allow you to format code at the touch of a button. Please do so before posting code here - it'll make it easier to help you, and a better question to help other people in the future. – Jon Skeet Aug 19 '16 at 07:45
  • @JonSkeet, I'm using netbean .. I'm still new using it. I'll google it . Thanks for the heads up – Jeev Aug 19 '16 at 07:46
  • @JBNizet, I'm getting weird errors like this run: Error: unable to load driver class! C:\Users\Jeeva\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) But when i check it shows is my netbean but when i run other java file it's no problem. – Jeev Aug 19 '16 at 07:46
  • 3
    If you're this new to Java. I would *strongly* suggest you start with basic console apps, getting the hang of the very basics of the language before you start trying to use databases. – Jon Skeet Aug 19 '16 at 07:47

2 Answers2

3

You need to first instantiate object.

new JavaApplication2().outluck();

Static methods belong to class. Non-static methods belong to objects (I want to know the difference between static method and non-static method).

Community
  • 1
  • 1
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
  • Thanks tomek for the help , when i try compile it. The error that return was : Error: unable to load driver class! C:\Users\Jeeva\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) – Jeev Aug 19 '16 at 07:43
  • look here: http://stackoverflow.com/questions/33734791/every-java-program-i-try-to-start-shows-error – Tomasz Mularczyk Aug 19 '16 at 07:46
  • Tomek , so it's not my code problem? Phew thanks man. I've been confusing google and i didn't gotten to that post . – Jeev Aug 19 '16 at 07:48
  • yep, seems like the same error youre having – Tomasz Mularczyk Aug 19 '16 at 07:53
0

Non Static parameters and methods needs to be called with Objects.

Try creating a Object for JavaApplication2 and invoke the method.

    JavaApplication2 obj = new JavaApplication2();
    obj.outluck();
Vishal Santharam
  • 1,963
  • 1
  • 16
  • 30
  • Thanks bro for the assist, but when i compile i also get this error : run: Error: unable to load driver class! C:\Users\Jeeva\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) – Jeev Aug 19 '16 at 07:44