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: How do I use a variable from main class in another class?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    Spain
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I use a variable from main class in another class?

    Hello. First of all, hope I'm not writing this in the wrong forum (Doubted about posting this at Java Theory). I'm working on a project where I have a string builder in my main class:

    public class Main {
    public static void main(String[] args) {

    final StringBuilder log=new StringBuilder();

    }
    }

    So I wanna use log in a different class and I have no clue on how to. I tried something like this, but it just keeps ending as an error:

    public class Car extends Thread{

    Main.log.append("Whatever");

    }

    Can somebody help me?

    Thanks in advance.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: How do I use a variable from main class in another class?

    Your problem is that the log variable is created inside the main(String[] args) method. You have to create this object outside the method but inside the class.

    public class Main {
     
    final StringBuilder log;
     
    public static void main(String[] args) {
     
    log=new StringBuilder();
     
    }
    }

    PS. Put your code in CODE tags starting with CODE=java within squarebrackets and ending with /CODE within squarebrackets.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Location
    Spain
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I use a variable from main class in another class?

    Oh god, I should had thought that.

    Thank you very much, and sorry about the code thing

Similar Threads

  1. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. Replies: 5
    Last Post: October 18th, 2012, 01:43 PM
  4. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  5. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM