Calling Instance methods from main

Hello all,

I am learning java for the first time after finishing a course in windows application programming in C#.

I am starting with a simple program and i would like to know how i can call instance methods from within the main function (static).

Here is my code, i have not included all my source files since i believe they are not relevant to the question...

Every time i compile the following code i will get the error telling me that i cannot call the non-static method CreateEmployee() from a static reference.

Can anyone tell me how i would call this method from my main function?

Many Thanks,

Oli

package lab2;

import java.util.Scanner;

publicclass Lab2Testextends WeeklyEmployee

{

public Lab2Test(String employeeNumber,String name, String address,int age,int basicSalary,double hoursPerWeek, String firstClassKey)

{

super(employeeNumber, name, address, age, basicSalary, hoursPerWeek);

}

publicstaticvoid main(String args[])

{

int option;

Scanner in =new Scanner(System.in);

System.out.println("1: Create Employee");

System.out.println("2: Delete Employee");

System.out.println("3: Employee Gross Pay");

System.out.println("4: Employee Net Pay");

System.out.println("5: Employee Tax");

option = in.nextInt();

switch(option)

{

case 1:

CreateEmployee();

in.close();

break;

case 2://DeleteEmployee();

in.close();

break;

case 3://EmployeeGross();

in.close();

break;

case 4://EmployeeNet();

in.close();

break;

case 5://EmployeeTax();

in.close();

break;

default:;

}

}

publicvoid CreateEmployee()

{

System.out.println("Enter Employee Name: ");

}

/*

public void DeleteEmployee()

{

System.out.println("Delete Employee");

}

public void EmployeeGross()

{

System.out.println("Employee Gross Pay");

}

public void EmployeeNet()

{

System.out.println("Employee Net Pay");

}

public void EmployeeTax()

{

System.out.println("Employee Tax");

}*/

}

Message was edited by:

McPhee

[3833 byte] By [McPheea] at [2007-11-26 18:37:50]
# 1

> I am starting with a simple program and i would like

> to know how i can call instance methods from within

> the main function (static).

Create an instance.

class Foo {

... static ... main(...) {

Foo foo = new Foo();

foo.instanceMethod();

}

jverda at 2007-7-9 6:11:54 > top of Java-index,Java Essentials,New To Java...
# 2

Hello,

Thank-you for the quick reply,

when i create an instance i receive the following compile error:

cannot find symbol

symbol : class Lab2test

location: class lab2.Lab2Test

case 1: Lab2Test ins = new Lab2test();

1 error

BUILD FAILED (total time: 0 seconds)

Message was edited by:

McPhee

McPheea at 2007-7-9 6:11:54 > top of Java-index,Java Essentials,New To Java...
# 3
These are not the same. Java is case-sensitiveLab2TestLab2test
jverda at 2007-7-9 6:11:54 > top of Java-index,Java Essentials,New To Java...
# 4

oops, silly mistake,

However, after changing the problem still persists...

cannot find symbol

symbol : constructor Lab2Test()

location: class lab2.Lab2Test

case 1: Lab2Test ins = new Lab2Test();

1 error

BUILD FAILED (total time: 0 seconds)

Here is how i implemented it:

case 1: Lab2Test ins = new Lab2Test();

ins.CreateEmployee();

in.close();

break;

Message was edited by:

McPhee

McPheea at 2007-7-9 6:11:54 > top of Java-index,Java Essentials,New To Java...
# 5
Read the error messages carefully. They tell you what's going on.You're trying to create the object with a constructor that takes no arguments. There is no such constructor in your class because you've defined one or more constructors that take arguments.
jverda at 2007-7-9 6:11:54 > top of Java-index,Java Essentials,New To Java...
# 6

Thank-you jverd

I solved it by assigning some static variables to the class and using them in the Lab2Test call...

eg.

* Lab2Test.java

*

* Created on 12 February 2007, 17:02

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package lab2;

import java.util.Scanner;

public class Lab2Test extends WeeklyEmployee

{

private static String employeeNumber;

private static String name;

private static String address;

private static int age;

private static int basicSalary;

private static double hoursPerWeek;

private static String firstClassKey;

public Lab2Test(String employeeNumber,String name, String address, int age, int basicSalary, double hoursPerWeek, String firstClassKey)

{

super(employeeNumber, name, address, age, basicSalary, hoursPerWeek);

}

public static void main(String args[])

{

int option;

Scanner in = new Scanner(System.in);

System.out.println("1: Create Employee");

System.out.println("2: Delete Employee");

System.out.println("3: Employee Gross Pay");

System.out.println("4: Employee Net Pay");

System.out.println("5: Employee Tax");

option = in.nextInt();

switch(option)

{

case 1: Lab2Test ins = new Lab2Test(employeeNumber,name,address,age,basicSalary,hoursPerWeek,firstClassKey);

ins.CreateEmployee();

in.close();

break;

case 2: //DeleteEmployee();

in.close();

break;

case 3: //EmployeeGross();

in.close();

break;

case 4: //EmployeeNet();

in.close();

break;

case 5: //EmployeeTax();

in.close();

break;

default:;

}

}

public void CreateEmployee()

{

Scanner in = new Scanner(System.in);

System.out.println("Enter Employee Name: ");

String nameEmployee = "";

nameEmployee = in.nextLine();

super.setName(nameEmployee);

System.out.println(super.getName());

}

}

I hope this is the right way to do it!

it seems to work.

McPheea at 2007-7-9 6:11:54 > top of Java-index,Java Essentials,New To Java...
# 7
> I solved it by assigning some static variables to the classAh yes, that is always the solution. NOT!
floundera at 2007-7-9 6:11:54 > top of Java-index,Java Essentials,New To Java...
# 8
Hmmm... lol.Not sure if you're making a quip on the method i used to solve my problem, or whether you were attempting a generic 'funny' about my solution being the answer to all problems.i'll assume the latter... lol
McPheea at 2007-7-9 6:11:54 > top of Java-index,Java Essentials,New To Java...
# 9

I was telling you that your solution is wrong. The variables should not be static as they should belong to a particular instance of the class, not the class itself. Also, if your class extends WeeklyEmployee (even though it shouldn't becuase Lab2Test is not a specific type of a Weekly Employee, ie Labrador would extend Dog) then you should not be redeclaring the variables that your subclass inherits from the super class.

floundera at 2007-7-9 6:11:54 > top of Java-index,Java Essentials,New To Java...
# 10
Hmmm... this is a much more helpful reply than your previous one!I am still learning so advice is useful - sarcasm isnt.
McPheea at 2007-7-9 6:11:54 > top of Java-index,Java Essentials,New To Java...