Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8854505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:53:48+00:00 2026-06-14T13:53:48+00:00

Possible Duplicate: What is the reason behind non-static method cannot be referenced from a

  • 0

Possible Duplicate:
What is the reason behind “non-static method cannot be referenced from a static context”?

I am new to Java. I have following sets of code as below.

class Default
{
    private short s;
    private int i;
    private long l;
    private float f;
    private double d;
    private char c;
    private String str;
    private boolean b;

    public static void main (String args[ ])
    {

        Default df = new Default();

        System.out.println("\n Short = "+s);

        System.out.println ("\n int i =" + i);
        System.out.println ("\n long l =" + l );
        System.out.println ("\n float f =" + f);
        System.out.println ("\n double d =" + d);
        System.out.println ("\n char c =" + c);
        System.out.println ("\n String s =" + str);
        System.out.println("\n boolean b =" + b);
    }
}

This produces an error message as the subject of this question but following code works perfectly.

class Default
{
    private short s;
    private int i;
    private long l;
    private float f;
    private double d;
    private char c;
    private String str;
    private boolean b;

    public static void main (String args[ ])
    {

        Default df = new Default();

        System.out.println("\n Short = "+df.s);

        System.out.println ("\n int i =" + df.i);
        System.out.println ("\n long l =" + df.l );
        System.out.println ("\n float f =" + df.f);
        System.out.println ("\n double d =" + df.d);
        System.out.println ("\n char c =" + df.c);
        System.out.println ("\n String s =" + df.str);
        System.out.println("\n boolean b =" + df.b);
    }
}

This gives the desired result. What is the difference in these two set of code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:53:50+00:00Added an answer on June 14, 2026 at 1:53 pm

    You have an instantiated object of Default named df which is calling those variables. Since the variables you created are not static, they must be apart of some object that has been created.

    Default df = new Default();
    df.i //<- is now a part of the instantiated object df.
    

    You use the keyword static if it is not used with an object. So you could just say:

    private static char c;
    

    And then you can call char c anytime, which will be null because you haven’t given it a value yet.

    You use static when you will be using that variable or method without an object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is the reason behind “non-static method cannot be referenced from a
Possible Duplicate: What is the reason behind “non-static method cannot be referenced from a
Possible Duplicate: Why is the Java main method static? What's the reason of main
Possible Duplicate: Any reason to overload global new and delete? In c++ you can
Possible Duplicate: What’s the reason I can’t create generic array types in Java? HashSet<Integer>[]
Possible Duplicate: Why aren't static const floats allowed? Is there any reason why this
Possible Duplicate: What is System.Void? I have no practical reason for knowing this answer,
Possible Duplicate: Cannot refer to a non-final variable inside an inner class defined in
Possible Duplicate: Are do-while-false loops common? Is there a reason to have code like:
Possible Duplicate: Are Getters and Setters evil? I can't find a logical reason behind

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.