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 3 of 3

Thread: non static variable this cannot be referenced from a static context

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation non static variable this cannot be referenced from a static context

    public class Main extends Thread
    {

    class credit extends Thread
    {
    public void run()
    {


    String filename="Credit_card.txt";
    String path_Training_DataSet="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Main\\src\\D atasets"+filename;
    String path_Attribute_Information="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Main\\src\\D atasets\\Attribute_information.data";

    String dlimiter=",";
    Dataset training_DataSet= new Dataset(path_Training_DataSet,dlimiter,path_Attrib ute_Information,dlimiter,"training");

    }
    }
    class debt extends Thread
    {
    public void run()
    {
    String filename="Debt_consolidations.txt";
    String path_Training_DataSet="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Main\\src\\D atasets"+filename;
    String path_Attribute_Information="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Main\\src\\D atasets\\Attribute_information.data";

    String dlimiter=",";
    Dataset training_DataSet= new Dataset(path_Training_DataSet,dlimiter,path_Attrib ute_Information,dlimiter,"training");

    }

    }
    class education extends Thread
    {
    public void run()
    {
    String filename="Educational_loan.txt";
    String path_Training_DataSet="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Main\\src\\D atasets"+filename;
    String path_Attribute_Information="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Main\\src\\D atasets\\Attribute_information.data";

    String dlimiter=",";
    Dataset training_DataSet= new Dataset(path_Training_DataSet,dlimiter,path_Attrib ute_Information,dlimiter,"training");

    }
    }

    class home extends Thread
    {
    public void run()
    {
    String filename="Home_loan.txt";
    String path_Training_DataSet="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Main\\src\\D atasets"+filename;
    String path_Attribute_Information="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Main\\src\\D atasets\\Attribute_information.data";

    String dlimiter=",";
    Dataset training_DataSet= new Dataset(path_Training_DataSet,dlimiter,path_Attrib ute_Information,dlimiter,"training");

    }


    }
    public static void main(String args[])
    {
    long start=System.currentTimeMillis();
    try
    {
    String filename="Small_Business.txt";
    String path_Training_DataSet="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Main\\src\\D atasets"+filename;
    String path_Attribute_Information="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Main\\src\\D atasets\\Attribute_information.data";

    String dlimiter=",";
    Dataset training_dataset= new Dataset(path_Training_DataSet,dlimiter,path_Attrib ute_Information,dlimiter,"training");


    ************************ERROR********************* *******
    credit t1=new credit(); //here it shows non static variable, this cannot be referenced from a static context
    debt t2=new debt();
    education t3=new education();
    home t4=new home();
    ************************ERROR********************* *******


    t1.start();
    t2.start();
    t3.start();
    t4.start();

    t1.join();
    t2.join();
    t3.join();
    t4.join();




    }
    catch(Exception e)
    {
    System.out.println("Exception happened");
    }


    long end=System.currentTimeMillis();
    System.out.println("Required time="+(end-start));
    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: non static variable this cannot be referenced from a static context

    main is a static method. It can only reference static elements in the source. Change the Credit class to static to allow main to access it.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: non static variable this cannot be referenced from a static context

    ok thank you !

Similar Threads

  1. [SOLVED] Need help desperately: "non-static variable this cannot be referenced from a static context"
    By mikbferguson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2017, 08:27 PM
  2. Replies: 6
    Last Post: May 3rd, 2013, 04:25 PM
  3. Replies: 4
    Last Post: November 15th, 2012, 12:09 AM
  4. [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
  5. 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

Tags for this Thread