Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: non-static method cannot be referenced from a static context

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default non-static method cannot be referenced from a static context

    This will be the first post of me life on a forum of any type so please bare with me, and I will learn the proper etiquette.

    I am working on an Address book program for a class I am in.

    I am getting a " Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static method menu() cannot be referenced from a static context at addressbook.AddressBook.main" that I can not figure out.

    I need a point in the right direction.

    I have read and reread about instance variables trying to figure this out.

    In order to get everything flying I have to start it from my main() which must be static, but none of my methods are static and cannot be accessed from a static context.

    my code follows.
    AddressBook.java
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package addressbook;
     
    import java.util.Scanner;
     
    /**
     *
     * @author Eagle
     */
    public class AddressBook {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            menu();
        }
     
        public void menu() {
            Scanner sIn = new Scanner(System.in);
            //Menu
            System.out.println("----/ / /  MENU  \\ \\ \\----");
            System.out.println();
            System.out.println("1.) Add Buisness Contact      ");
            System.out.println("2.) Add Personal Contact      ");
            System.out.println("3.) Display Buisness Contacts ");
            System.out.println("4.) Display Personal Contacts ");
            System.out.println("5.) Quit ");
     
            int choice = sIn.nextInt();
            if (choice == 1) {
                BusinessContact.addContact();
            } else if (choice == 2) {
                PersonalContact.addContact();
            } else if (choice == 3) {
                BusinessContact.printContact();
            } else if (choice == 4) {
                PersonalContact.printContact();
            } else if (choice == 5) {
                System.exit(0);
            } else {
                System.out.println("Please enter a number between 1 and 5.");
            }
        }
     
    }

    Contact.java
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package addressbook;
     
    import java.util.ArrayList;
    import java.util.Scanner;
     
    /**
     *
     * @author Eagle
     */
    public abstract class Contact {
     
        private String Fname;
        private String Lname;
        private String Add;
        private String Pnumber;
        private String Eadd;
        Scanner stdIn = new Scanner(System.in);
     
     
     
        public Contact(String FirstName, String LastName, String Address, String PhoneNumber, String EmailAddress){
            Fname = FirstName;
            Lname = LastName;
            Add = Address;
            Pnumber = PhoneNumber;
            Eadd = EmailAddress;
     
        }
     
        abstract void addContact();
     
        public void printContact(){
            System.out.println("Name: "+ Fname + " " + Lname);
            System.out.println("Address: " + Add);
            System.out.println("Phone: " + Pnumber);
            System.out.println("E-Mail Address: " + Eadd);
        }
     
        String getFname(){
            return Fname;
        }
     
        void setFname(String newValue){
            Fname = newValue;
        }
     
        String getLname(){
            return Lname;
        }
     
        void setLname(String newValue){
            Lname = newValue;
        }
     
        String getAdd(){
            return Add;
        }
     
        void setAdd(String newValue){
            Add = newValue;
        }
     
        String getPnumber(){
            return Pnumber;
        }
        void setPnumber(String newValue){
            Pnumber = newValue;
        }
     
        String getEadd(){
            return Eadd;
        }
     
        void setEadd(String newValue){
            Eadd = newValue;
        }
    }

    PersonalContact.java
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package addressbook;
     
    /**
     *
     * @author Eagle
     */
    public class PersonalContact extends Contact {
        private String Dob;
     
     
        public PersonalContact(String FirstName, String LastName, 
                               String Address, String PhoneNumber, 
                               String EmailAddress, String DateofBirth) {
     
            super(FirstName, LastName, Address, PhoneNumber, EmailAddress);   
            Dob = DateofBirth;
     
        }
     
        /**
         *
         */
        @Override
        public void addContact(){
     
     
        }
        String getDob(){
            return Dob;
        }
     
        void setDob(String newValue){
            Dob = newValue;
        }
     
        @Override
        public void printContact() {
            super.printContact();
            System.out.println("Date of Birth: " + Dob);
        }
    }

    BusinessContact.java
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package addressbook;
     
    /**
     *
     * @author Eagle
     */
    public class BusinessContact extends Contact {
        private String Jtitle;
        private String Org;
     
        public BusinessContact(String FirstName, String LastName, 
                               String Address, String PhoneNumber, 
                               String EmailAddress, String JobTitle, 
                               String Organization) {
     
            super(FirstName, LastName, Address, PhoneNumber, EmailAddress);   
            Jtitle = JobTitle;
            Org = Organization;
        }
     
     
        @Override
        public void printContact(){
            super.printContact();
            System.out.println("Job title: " + Jtitle);
            System.out.println("Organization: " + Org);
        }
     
        @Override
        public void addContact(){
     
     
        }
     
        String getJtitle(){
            return Jtitle;
        }
     
        void setJtitle(String newValue){
            Jtitle = newValue;
        }
     
        String getOrg(){
            return Org;
        }
     
        void setOrg(String newValue){
            Org = newValue;
        }
    }

    Thank you for any help

    James K "Soaring Eagle" Linderman


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: non-static method cannot be referenced from a static context

    The main() method HAS to be static. In other words, it 'exists' without requiring an instance of its enclosing class. This is how the main() method can be the entry point for a Java program. Static methods (or class methods), like main(), can only call other static methods. That ensures that the two methods will 'exist' in the same, static state or space. Your menu() method is not static, so needs an instance of it's enclosing class to exist. Or, to make it available to the main() method without creating an instance of the enclosing class, put the modifier 'static' in the menu() signature. You can learn more about static or class methods here and many other places on the Internet. It's a tough concept to get at first.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    jlinder (December 6th, 2013)

Similar Threads

  1. Replies: 6
    Last Post: May 3rd, 2013, 04:25 PM
  2. Replies: 4
    Last Post: November 15th, 2012, 12:09 AM
  3. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM