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

Thread: Question about classes...

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Question about classes...

    Hey, I've finally almost finished my Java class at school and I've started going off on my own trying to learn how to program various things. I'm currently trying to find out how to run a specific method of a class from another class. I attempted to look up how to do this but I must be searching with the wrong terms or something.

    Example:

    -I open up the cmd and start up First.class
    -Somewhere in First.class it may need to run a method from Second.class, maybe to do a calculation or something. (This is probably a bad example...)

    How would I go about running a specific method from Second.class through First.class?

    Another question that I just thought of while typing up the above question is; Can I, if the above question is possible to do, move varibles and their values from one class to another and how would I go about doing that?




    Thanks in advance for any help, tutorial links are also welcome!


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

    Default Re: Question about classes...

    I've finally almost finished my Java class at school
    How long a course was it? How did it not cover these topics?

    How would I go about running a specific method from Second.class through First.class?
    Get a reference to Second class and use it to call the method:
    refToSecondClass.theMethodToCall();

    move varibles and their values from one class to another
    To move variables from one source to another, use an editor. Cut the lines defining the variables out of one source file and paste them into the other file.
    If you don't understand my answer, don't ignore it, ask a question.

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

    tyeeeee1 (December 1st, 2012)

  4. #3
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: Question about classes...

    Quote Originally Posted by Norm View Post
    How long a course was it? How did it not cover these topics?

    Get a reference to Second class and use it to call the method:
    refToSecondClass.theMethodToCall();


    To move variables from one source to another, use an editor. Cut the lines defining the variables out of one source file and paste them into the other file.

    It's a 5 month long grade 12 online course. So-far it's covered Running Java Programs, Data Types, Working with Variables, Input and Output, Branches, Loops, OOP, Applets and Animation, Strings and the two sections I have left to do are Arrays & OOP Designing with Inheritance. The course seems a bit lacking, but it's taught me how to read the code and make basic programs.

    Thanks for the help, as for the last part where you say to CnP the variables from file to file; That would work if the variables are static, but what if they keep changing and the Second.class needs the latest values while First.class is running? (sorry if I misunderstood what you meant there.)

    Edit:
    So, to call a certain method I'd type notSureAboutThisPart.Main(); to call the main method of the second class?

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

    Default Re: Question about classes...

    to call the main method of the second class?
    Is the main() method static? That changes the how it is called. To call the standard main() method:
    TheClassNameHere.main(<a String array here>);
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    tyeeeee1 (December 1st, 2012)

Similar Threads

  1. Question about creating and using classes.
    By iDizzle in forum Object Oriented Programming
    Replies: 20
    Last Post: April 1st, 2012, 03:14 PM
  2. Replies: 4
    Last Post: October 24th, 2011, 08:09 AM
  3. i/o classes question
    By p0oint in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 28th, 2010, 04:04 PM
  4. noob question on abstract classes
    By joshred in forum Object Oriented Programming
    Replies: 1
    Last Post: September 15th, 2010, 06:27 PM
  5. A Question Regarding Abstract Classes & Packages
    By Kerubu in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 01:27 PM