Solved non-static method print(CAccount) cannot be referenced from a static context

Posted on 2007-10-19
Last Modified: 2010-08-05

I get the error below: non-static method print(CAccount) cannot be referenced from a static context
Question by:zizi21
    LVL 23

    Accepted Solution

    You need to create an instance of the class before you can access its non-static methods, for example:

    TestCAccount myclass = new TestCAccount();

    Author Comment

    Just wondering, if the main is static : public void static main(String [] args), i thought it is okay to make the print method static as well , but that did not work . If I do the instance of the class that would work. but in my lecture notes, there was no instance... we were learning on overriding and overloading...
    LVL 23

    Expert Comment

    well you could make the print method static, then you wouldn't get that error, but then if the print method itself makes references to non-static methods you'll get the same error in the print method and have to create an instance there...
    LVL 86

    Assisted Solution

    >>there was no instance... we were learning on overriding and overloading...

    Just to make it clear - static methods can be overloaded but not overridden

    Author Comment

    let me try it..i am a bit confused

    Author Comment

    sorry..just finished two other exams..going back to java..forgive me for the late reply...
    LVL 92

    Expert Comment

    You should typically be avoiding making things static.
    You main should be creating an instance of a class (or classes) and calling (non-static) methods on those instances.

    Author Comment


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    java constructor error 8 65
    copyEndy  challenge 15 40
    advance java 4 36
    What is the latest versions eclipse neon 2 59
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now