Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

How to link the two files?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am a total newbie to start to learn Java,. So many tutorials start with IDE. In order to get a clear understanding of how java work, I don't want  any distractions from  IDEs, I just want to do it in a plain text editor.
Now I create two files in BBedit, one is HelloWord.java

The other one is GoodbyeWorld.java


I compiled them individually, then run them, they both work fine.

Now I need to learn how to link the two files, so that when I run HelloWorld. It will output Goodbye, World instead. How do I use the method from the other class? What is the syntax?Thanks

 
Saloon Keeper
Posts: 6449
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you're asking is not typically done. While it is possible to call a main() method it is more typical to call another method like print() for example. To call main() as you are trying to do, you'd first have to comment out or delete the printing of "Hellow, World". Then, because main() is static it can be called like this. Note that an array of Strings is necessary because that is what main() is expecting for a parameter.
 
Carey Brown
Saloon Keeper
Posts: 6449
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would be a better approach; creating static print() methods to do the work.

 
tony baloni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:This would be a better approach; creating static print() methods to do the work.



Thank you for your reply. More question, sorry. Howcome in the second method, I don't need something like xyz = new GoodbyeWorld to create an object before I use its method?
 
Carey Brown
Saloon Keeper
Posts: 6449
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods don't operate on an object so you don't need one. If you change it to non-static (like I've done here), then you need to have an object, here created with 'new'.
 
Marshal
Posts: 66467
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's “Goodbye, Cruel World.”
 
Campbell Ritchie
Marshal
Posts: 66467
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey is right to change the method to non‑static because you get a more object‑oriented application. But I prefer to say, “instance,” rather than, “non‑static”. There is a compiler error message which says, “non‑static”, and I think that term causes confusion to beginners. You wil come across that error message soon enough.
 
tony baloni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you again for all you guys helps. Now it start to make sense to my little brain So what are the pros and cons in using static and non-static? In what situation would I use one or the other? Does it have to do with code security?
 
Carey Brown
Saloon Keeper
Posts: 6449
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that it's a pros vs cons thing, each has its place. A static has only one per class regardless of how many instances of an object you may have. You don't have an implicit object ("this") to operate on. Statics often indicate that you are not designing an Object Oriented program. Statics are useful for utility methods. Whole classes have been created with nothing but static utility methods in them. Instance ("non-static") variables and methods must operate on an instance of the class; an object. An object carries a state around with it so that instance methods have access to the state of the whole object. Instance (non-static) is what you need for Object Oriented programs.
 
tony baloni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. On my Mac in Terminal I use javac to compile my two classes. but in the non-static method. I need to compile the two files (both files are in the same directory) together so I use javac -d GoodbyeWorld.java HelloWorld.java
The Terminal gave
javac: not a directory: GoodbyeWorld.java
Usage: javac <options> <source files>
use -help for a list of possible options

What is the correct syntax for javac -d?
 
Carey Brown
Saloon Keeper
Posts: 6449
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Ranch Hand
Posts: 393
8
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tony baloni wrote:Hello, I am a total newbie to start to learn Java,. So many tutorials start with IDE. In order to get a clear understanding of how java work, I don't want  any distractions from  IDEs, I just want to do it in a plain text editor.



Your point  about starters and heavy IDE's is absolutely right i can say i hear it only rarely trough starters.But you have to know other than heavy/professional IDE's(e.g. NetBeans, Eclipse, IntelliJ)  out there exist also very simple "IDE's" specifically thought and implemented for students/starters(for teaching/learning) e.g. BlueJ and  DrJava . They are written in java and offer only a bit more than a plain text editor so no distraction

Apart  your specific question here are few pointers to help you go further:
1. It's recommended when one start to learn OO Programming other than start learn the language itself he/she choose,  start learn also about OO Design. Really the later in my humble opinion is more important and applicable to any language.  

2. take a look in that thread OOP, what is an Object? i guess you will find it useful

3. Take a look (often) in our small(in expansion) library of good open texts ; start look at the section Beginners books  subsections OO related and Java; Everything you choose there it's good  
If you have no idea where to start get a try  for example at
  • Think Java How to Think Like a Computer Scientist 2002 by Allen B. Downey
  • Introduction to Programming Using Java, Version 7.0, August 2014 by David J. Eck
  • Object-Oriented Programming used Java 2006 by David J. Eck, et al
  • Object-oriented Programming in JavaTM Textbook by Richard L. Halterman 2008(use DrJava)
  • OOP - Learn Object Oriented Thinking & Programming 2013 by Rudolf Pecinovsky (use BlueJ)


  • Downey's small textbook is almost ideal for starters(is small and uses the necessary Java's subset for start and avoid student's overwhelming) i guess  
    The last (Pecinovsky) is made  in a particular way (respectively of other traditional textbooks) trough dialogs between an hypothetical student/learner and his teacher

    And ... Welcome to Ranch!  

     
    tony baloni
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Harry Kar wrote:

    tony baloni wrote:Hello, I am a total newbie to start to learn Java,. So many tutorials start with IDE. In order to get a clear understanding of how java work, I don't want  any distractions from  IDEs, I just want to do it in a plain text editor.



    Your point  about starters and heavy IDE's is absolutely right i can say i hear it only rarely trough starters.But you have to know other than heavy/professional IDE's(e.g. NetBeans, Eclipse, IntelliJ)  out there exist also very simple "IDE's" specifically thought and implemented for students/starters(for teaching/learning) e.g. BlueJ and  DrJava . They are written in java and offer only a bit more than a plain text editor so no distraction

    Apart  your specific question here are few pointers to help you go further:
    1. It's recommended when one start to learn OO Programming other than start learn the language itself he/she choose,  start learn also about OO Design. Really the later in my humble opinion is more important and applicable to any language.  

    2. take a look in that thread OOP, what is an Object? i guess you will find it useful

    3. Take a look (often) in our small(in expansion) library of good open texts ; start look at the section Beginners books  subsections OO related and Java; Everything you choose there it's good  
    If you have no idea where to start get a try  for example at
  • Think Java How to Think Like a Computer Scientist 2002 by Allen B. Downey
  • Introduction to Programming Using Java, Version 7.0, August 2014 by David J. Eck
  • Object-Oriented Programming used Java 2006 by David J. Eck, et al
  • Object-oriented Programming in JavaTM Textbook by Richard L. Halterman 2008(use DrJava)
  • OOP - Learn Object Oriented Thinking & Programming 2013 by Rudolf Pecinovsky (use BlueJ)


  • Downey's small textbook is almost ideal for starters(is small and uses the necessary Java's subset for start and avoid student's overwhelming) i guess  
    The last (Pecinovsky) is made  in a particular way (respectively of other traditional textbooks) trough dialogs between an hypothetical student/learner and his teacher

    And ... Welcome to Ranch!  



    Thanks for your input. I remember in the old days when I started programming in then Obj-C for iOS. I started with XCode. It was quite a struggle joggling between XCode and Obj-C . Later on I realized that there were a lot of basics I don't understand., such the compile and linking process and program structure because XCode did all that for me. I am going to learn the "hard" way-with just a simple good old text editor and the Terminal, so that I have better understanding
    As for as OOP concern, I think I have a pretty good grab on it, but I will read some of the books recommended.
    Now as for the usage of javac -d, I still need some time to search for the answer.
     
    Harry Kar
    Ranch Hand
    Posts: 393
    8
    Open BSD BSD Debian
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    tony baloni wrote:
    Thanks for your input.



    You're welcome

    I remember in the old days when I started programming in then Obj-C for iOS. I started with XCode. It was quite a struggle joggling between XCode and Obj-C . Later on I realized that there were a lot of basics I don't understand., such the compile and linking process and program structure because XCode did all that for me. I am going to learn the "hard" way-with just a simple good old text editor and the Terminal, so that I have better understanding



    I quote you're on the right way

    As for as OOP concern, I think I have a pretty good grab on it, but I will read some of the books recommended.


    Ah in that case one of the recommended texts earlieror those in the books list  can be useful for a repass the basic OO concepts in Java lingo. For further knowledge on OO Design you can find interesting material in the  OO Design Books Wiki

    Now as for the usage of javac -d, I still need some time to search for the answer.


    Why you want place the compiled files(.class) in another directory? As Carey pointed out normally one goes into the directory where reside the .java source files open a shell and compile there then the .class files are placed in the very same directory (as the .java files) and that's it
    Moreover if almost one of the source files have dependencies of another source file the compilation takes place recursively mean if SourceFile1 refer to a class that reside in SourceFile2 is enough type  javac Sourcefile1 and you have automatically compiled (other than Sourcefile1)SourceFile2 too

    Anyway Java's docs describe javac -d  well enough

    PS: for the next time gets better your quoting makes your post easier to read for others.Mean not quote integrally but quote only the parts you want to respond/discuss e.g. as i did on that post
     
    Harry Kar
    Ranch Hand
    Posts: 393
    8
    Open BSD BSD Debian
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    tony baloni wrote: Thank you again for all you guys helps. Now it start to make sense to my little brain So what are the pros and cons in using static and non-static? In what situation would I use one or the other? Does it have to do with code security?



    For a very first intro:
    Static members(fields, methods) are "sticked" with the class concept(that's a static thing) as opposed to instance members where are "sticked" with the object concept(that's a dynamic thing). In OOP all the work is carried out from objects not from classes(that only serve as object's templates/fabrics )
    As also Carey pointed out normally who is interested in a OO language is also interested to apply the OO designs trough that language. For that matter initially you can very well abstract about the existence of static members (fields(data) + methods(functions/procedures))  and postpone their treatment for later
     
    Sheriff
    Posts: 6427
    172
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    tony baloni wrote:Now as for the usage of javac -d, I still need some time to search for the answer.


    From javac --help -d:
    So you need to specify the "target" directory.
     
    Campbell Ritchie
    Marshal
    Posts: 66467
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I believe the −d option to javac will create the required directory if it doesn't already exist. But I haven't used −d for ages.
     
    Harry Kar
    Ranch Hand
    Posts: 393
    8
    Open BSD BSD Debian
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I believe the −d option to javac will create the required directory if it doesn't already exist. But I haven't used −d for ages.



    No the directory must already exist

    -d directory

       Sets the destination directory for class files. The directory must already exist because javac does not create it. If a class is part of a package, then javac puts the class file in a subdirectory that reflects the package name and creates directories as needed.

       If you specify -d C:\myclasses and the class is called com.mypackage.MyClass, then the class file is C:\myclasses\com\mypackage\MyClass.class.

       If the -d option is not specified, then javac puts each class file in the same directory as the source file from which it was generated.

       Note: The directory specified by the -d option is not automatically added to your user class path.

     
    tony baloni
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:This would be a better approach; creating static print() methods to do the work.



    I followed Carey Brown' instruction I created two files HelloWorld.java and GoodbyWorld.java I put them in a folder called java_exercises than I cd to this directory,  javac them. When I did javac HelloWorld.java it generate a HelloWorld.class as well as a GoodbyeWorld.class, But when I try to run it. java HelloWorld, the terminal give this error:
    Error: Could not find or load main class .Users.tony.Desktop.java_exercises.HelloWorld
    What have I done wrong?


     
    Carey Brown
    Saloon Keeper
    Posts: 6449
    61
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    tony baloni wrote:I followed Carey Brown' instruction I created two files HelloWorld.java and GoodbyWorld.java I put them in a folder called java_exercises than I cd to this directory,  javac them. When I did javac HelloWorld.java it generate a HelloWorld.class as well as a GoodbyeWorld.class, But when I try to run it. java HelloWorld, the terminal give this error:
    Error: Could not find or load main class .Users.tony.Desktop.java_exercises.HelloWorld
    What have I done wrong?


    Did you cd into the same directory to run the program?

    You aren't declaring any 'package's in the .java files?

    You could try adding in class path:
     
    Bartender
    Posts: 3660
    151
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Harry Kar wrote: (...) Your point  about starters and heavy IDE's is absolutely right


    Relatively right, since not all agree.
     
    Campbell Ritchie
    Marshal
    Posts: 66467
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Harry Kar wrote:. . . No the directory must already exist  . . .

    I have just tried it with a non‑existent directory and a new directory was created. Maybe the behaviour is OS‑dependent, but I don't think so.
     
    Campbell Ritchie
    Marshal
    Posts: 66467
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think I meant something different from what the documentation says. The documentation ways that you have to start from an extant directory; what I meant is that starting from an extant directory,, a directory structure corresponding to the package structure is created.
     
    tony baloni
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    tony baloni wrote:I followed Carey Brown' instruction I created two files HelloWorld.java and GoodbyWorld.java I put them in a folder called java_exercises than I cd to this directory,  javac them. When I did javac HelloWorld.java it generate a HelloWorld.class as well as a GoodbyeWorld.class, But when I try to run it. java HelloWorld, the terminal give this error:
    Error: Could not find or load main class .Users.tony.Desktop.java_exercises.HelloWorld
    What have I done wrong?


    Did you cd into the same directory to run the program?

    You aren't declaring any 'package's in the .java files?

    You could try adding in class path:





    After some trial and error, I solved the problem. Yes I did cd into the correct directory and I didn't have to use any packages at all. However, I use the same approach on this :




    I get the folowing errors
    javac HelloWorld.java
    ./GoodbyeWorld.java:3: error: non-static method print() cannot be referenced from a static context
           print();
           ^
    javac GoodbyeWorld.java
    GoodbyeWorld.java:3: error: non-static method print() cannot be referenced from a static context
           print();
           ^

     
    Carey Brown
    Saloon Keeper
    Posts: 6449
    61
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    True. Comment out line 3.
     
    tony baloni
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:
    True. Comment out line 3.



    I am glad you point that out. In fact the entire method

    is not needed, I wasn't sure if I was correct.
     
    Harry Kar
    Ranch Hand
    Posts: 393
    8
    Open BSD BSD Debian
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Piet Souris wrote:

    Harry Kar wrote: (...) Your point  about starters and heavy IDE's is absolutely right


    Relatively right, since not all agree.



    AFAIK from a pedagogical perspective is demonstrated that the burden due to relatively complex tools (like heavy IDE's and others) is not necessary almost at the very first stages of learning. There's  lots of concepts to learn first (all the forces are pointed in that direction) before go to that tools
     
    Harry Kar
    Ranch Hand
    Posts: 393
    8
    Open BSD BSD Debian
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Harry Kar wrote:. . . No the directory must already exist  . . .

    I have just tried it with a non‑existent directory and a new directory was created. Maybe the behaviour is OS‑dependent, but I don't think so.



    Are you sure? in what platform are you? I assure you that's not correct almost on 3 ditinct *nix like systems i have tried it out
    I have a file in  ..  /test/java/HelloWorld.java and compile from test directory and want to put the class file into the project directory



    Without create a project directory i get:


    After created the project directory instead i get what i intend i.e. project/com/mypackage/HelloWorld.class


     
    Campbell Ritchie
    Marshal
    Posts: 66467
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think you have proven that we are in agreement. You are showing compilation with no source directory failing, and that isn't what I meant. I meant that the destination directory is created by the −d option.
     
    Harry Kar
    Ranch Hand
    Posts: 393
    8
    Open BSD BSD Debian
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I think you have proven that we are in agreement. You are showing compilation with no source directory failing, and that isn't what I meant. I meant that the destination directory is created by the −d option.


    Yet not sure i get what you mean

    ..You are showing compilation with no source directory failing,..

     vs your previous post

    Campbell Ritchie wrote:

       Harry Kar wrote:
       . . . No the directory must already exist  . . .

    I have just tried it with a non‑existent directory and a new directory was created.



    are antithetic . Please post a concrete example
     
    If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!