Results 1 to 6 of 6
  1. #1
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default Another basic newbie question..

    I have been going over this for days now, Im sure it is very basic, but I have googled it over and over. I have java for dummies, school text book, and head first java, all of which have examples in them that almost look identical to this, but yet I still get an error.

    The error is : non-static variable, this cannot be referenced from a static context.

    Java Code:
    package payrollwk4;
    
    import java.util.Scanner;
    /**
     *
     * @author erikjd21
     */
    public class payrollwk4
    {
      
        public static void main(String[] args) 
        {
            Employee empinfo = new Employee();// The error happens here
            
            empinfo.getInfo();
        }
    
        public class Employee
        {
    
            Scanner input = new Scanner (System.in);
    
            String name;
    
             public void getInfo(){
    
                 System.out.println("Welcome to payroll manager!");
                 System.out.print("Please enter the employee's name:");
                 name = input.next();
            }
        }
    }
    This is the code that is giving the error. The part that is the error is where I am attempting to make an object of the employee class to use the method inside of it, to cause the information to print to the console. I feel that if I get this part down, the rest of the program will just flow together. Googling that error message says to create a constructor for the class to pull the method from. But I was under the impression that's what I was doing with the line that is giving me errors.

    If anyone can, please tell me what I am forgetting to do.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Don't put the class Employee inside the other class

    Java Code:
    public class Employee{
      constructor
      methods
      main(){
        create object
        call methods on the object
      }
    }

  4. #4
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    OMG I apologize for wasting your time. I didnt know that each class should be within its own file. None of the books suggest that.

    Thank you, making a new file fixed it

    For future reference, every time I create a new class, I should make a seperate file for it?

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    No, then you would have hundreds of files to accomplish what one class file could accomplish. If your class relates to the purpose of the class file, AKA Bank Account class has deposit, new account, withdraw classes you wouldn't want to have three files to accomplish this.

    Most, if not all of the examples you'll find in teaching books will have a class file and a tester file. More than 2 files is hardly needed when learning. Now when you embark on a programming project you might find yourself in the position to package more than one class file with your tester file.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Although I suppose it is a personal preference. I prefer to try and split the program up into smaller files. However, early on in programming it's probably very necessary since most classes will only be a couple hundred lines.

    @op: you can have multiple classes in one file, one class(the same as the file name) should be public

    Java Code:
    public class A{
      public static void main(String[] args){
         A a = new A();
         B b = new B();
         C c = new C();
         D d = new D();
      }
    }
    
    class B{} 
    class C{}
    class D{}

Similar Threads

  1. Basic JSP question..
    By casid in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-28-2009, 07:51 PM
  2. Basic question on OOP.
    By madthinker in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 08:09 PM
  3. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 06:44 PM
  4. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 07:30 PM
  5. basic question about files
    By oregon in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 03:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •