Problems with static methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandyw
    New Member
    • Mar 2007
    • 122

    #1

    Problems with static methods

    I having trouble with my project and need a little help please


    "The method inputEmployee cannot be declared static; static methods can only be declared in a static or top level type"
    Part of my code where the problem is occurring.
    Code:
    public class Employee {
        static ArrayList<Employee> arlist;
    	int empid = arlist.size();
    	/**
    	 * displayMatch inputs a keyword from the user. It then iterates through the
    	 * ArrayList of Employees and outputs each one to the screen if the Employee
    	 * information contains the keyword.
    	 */
    
    	public static Employee inputEmployee() {
    		Employee temp = null;
    
    		//prompt for data
    
    		Scanner input_flag = new Scanner(System.in);
    		System.out.println("Enter Employee Id Number AB1234==>");
    		String empid = input_flag.next();

    Here is the other half of the problem
    "The method main cannot be declared static; static methods can only be declared in a static or top level type"
    part of my code
    Code:
    public static void main(String[] args) {
    
            arlist = new ArrayList<Employee>();
    
            kbd = new Scanner(System.in);
    		int choice;
    		System.out.println("Make a Section: ");
    Any help would be great...

    thanks
    sandy
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by sandyw
    I having trouble with my project and need a little help please


    "The method inputEmployee cannot be declared static; static methods can only be declared in a static or top level type"
    Part of my code where the problem is occurring.
    Code:
    public class Employee {
        static ArrayList<Employee> arlist;
    	int empid = arlist.size();
    	/**
    	 * displayMatch inputs a keyword from the user. It then iterates through the
    	 * ArrayList of Employees and outputs each one to the screen if the Employee
    	 * information contains the keyword.
    	 */
    
    	public static Employee inputEmployee() {
    		Employee temp = null;
    
    		//prompt for data
    
    		Scanner input_flag = new Scanner(System.in);
    		System.out.println("Enter Employee Id Number AB1234==>");
    		String empid = input_flag.next();

    Here is the other half of the problem
    "The method main cannot be declared static; static methods can only be declared in a static or top level type"
    part of my code
    Code:
    public static void main(String[] args) {
    
            arlist = new ArrayList<Employee>();
    
            kbd = new Scanner(System.in);
    		int choice;
    		System.out.println("Make a Section: ");
    Any help would be great...

    thanks
    sandy
    The first problem probably comes from using the same variable name twice, empid. The second problem probably comes from using arlist again but I cant be sure because I dont see how arlist is declared.

    Comment

    Working...