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

Thread: Connect class with code?...?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Connect class with code?...?

    I am a beginner to java, and as I was reading my textbook I got confused....so I'm here.

    It says you can create your own classes which define methods, then you can write a separate code which invokes that new class you created and creates objects with reference to the new class. My question is, in my book they are shown in two files...how do you connect the two so the code knows where the class info is? Make sense?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Connect class with code?...?

    If I've understood your question, its quite simple; All that needs to be done is keep
    the source/class files in the same directory and it will be fine.

    ie.
    Take this directory:
    C:\Users\YourName\HelloWorld

    Then inside HelloWorld have two .java files
    MyFirstClass.java and MyFirstClassTest.java
    Then when you compile (via command line?)
    you'd type:
    C:\Users\YourName\HelloWorld\javac MyFirstClass.java
    C:\Users\YourName\HelloWorld\javac MyFirstClassTest.java

    Now the class files will have been created and will be in the same current directory and aslong as this is the case, they will find each other.
    When you run MyFirstClassTest with
    C:\Users\YourName\HelloWorld\java MyFirstClassTest
    It will run fine.

    When you get more into Java you can start reading about Packages.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    truebluecougarman (January 18th, 2011)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Connect class with code?...?

    Welcome to the forums truebluecougarman
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Connect class with code?...?

    Quote Originally Posted by newbie View Post
    If I've understood your question, its quite simple; All that needs to be done is keep
    the source/class files in the same directory and it will be fine.

    ie.
    Take this directory:
    C:\Users\YourName\HelloWorld

    Then inside HelloWorld have two .java files
    MyFirstClass.java and MyFirstClassTest.java
    Then when you compile (via command line?)
    you'd type:
    C:\Users\YourName\HelloWorld\javac MyFirstClass.java
    C:\Users\YourName\HelloWorld\javac MyFirstClassTest.java

    Now the class files will have been created and will be in the same current directory and aslong as this is the case, they will find each other.
    When you run MyFirstClassTest with
    C:\Users\YourName\HelloWorld\java MyFirstClassTest
    It will run fine.

    When you get more into Java you can start reading about Packages.

    Thats exactly what I was looking for, thank you!!!

  6. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Connect class with code?...?

    Quote Originally Posted by JavaPF View Post
    Welcome to the forums truebluecougarman
    Thanks!

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Connect class with code?...?

    truebluecougarman
    Are you a byzoo-er?

    (slightly off topic)
    Last edited by helloworld922; January 18th, 2011 at 08:12 PM.

  8. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Connect class with code?...?

    Quote Originally Posted by helloworld922 View Post
    Are you a byzoo-er?

    (slightly off topic)
    I am unfamiliar with that term...so probably not

Similar Threads

  1. Java Code to connect remote server
    By idealguy in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 3rd, 2012, 04:59 AM
  2. Cant connect with database
    By ronn1e in forum JDBC & Databases
    Replies: 1
    Last Post: January 4th, 2011, 05:45 PM
  3. Cannot Connect to Server
    By Brt93yoda in forum Java Networking
    Replies: 0
    Last Post: December 9th, 2010, 05:54 PM
  4. How to connect keyboard through COM?
    By yogesh01 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 6th, 2010, 05:00 AM
  5. Connect 4 involving minimax.
    By mandar9589 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2009, 10:52 AM