• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

calling classes (newbie to Java and Netbeans)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm just beginning to teach myself Java. I've gotten set up w/ Netbeans. I'm just getting confused about some basic terminology I think. I'm working through some examples in an online textbook. I copied the text for a class called TextIO from the textbook into a new project on Netbeans. It compiled just fine. Now I want to use that class in another program called PrintSquare. How do I call it? I don't get the syntax. Do I use an "import" statement in the program. The textbook (http://math.hws.edu/javanotes/c2/s6.html) talks a little about this, but I don't understand how to make the link. So that I can use the TextIO class in the PrintSquare code.

Please help!

Thanks.
 
Ranch Hand
Posts: 69
IntelliJ IDE Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure the TextIO class is part of the new project.

Then, all you have to do is create a TextIO object.



Then you can populate any instance variables you need.

ie.
 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear,
You first have to understand the four access modifiers
viz. coderanch, protected, package name or default and private
I would recommend Head First Java book to be your guide here.
Now coming to your question. In the Netbeans environment, You esentialy create a project and within that you keep all your classes, interfaces etc.
Lets say there is a Project called Beginners
Now within this you have one class file called Novice.java and another called Expert.java
The class Novice is declared as public and if i want to call it in Class Expert than i dont need the import statement.
So i suggest your best bet is to read about the class access modifiers and you will find the answer to your query
 
Marshal
Posts: 80613
467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Not sure; it depends where you get TextIO from. I see your link gives you instructions about it.

I would suggest you ought not to use NetBeans, but a text editor, and follow the hints in this post.
You can read more about imports in the Java Language Specification, which can be difficult to read, however.
I see your link has instructions about using the command line in ยง2.6.2: read them carefully. They also show how to get TextIO into your coding. I presume you set a system PATH when you installed Java. You will have no end of difficulty without a PATH. Don't set a system CLASSPATH.

To summarise what that link says:
Click on the TextIO link, and open whatever comes down with a text editor.
You should already have created a JavaPrograms or similar directory. Save that file in that directory as TextIO.java. Navigate to that folder with a command line (cd JavaPrograms or similar). Compile the file with javac TextIO.java.
Becasue TextIO does not declare a package name, you can use it without imports as long as you put all your .java files in the same directory and use it as an “unnamed package” If you follow that link you will find that unnamed packages are permitted for beginners' programs.

Be sure to tell us how you got on.
 
Tim Gordon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for your replies. I was happy to find a place to get some help as a very early beginner.

I'm curious why you suggest a text editor rather than Netbeans. It seems like that or Eclipse has lots of useful tools for debugging, organizing, etc. On the other hand, it does seem to be the origin (at least in part) of my previously mentioned problem: my question about calling TextIO is partly about Java syntax and partly about using Netbeans.

Bottom line here is I'm still stuck. I've attached a screenshot of Netbeans showing the PrintSquare program. Look at the second line of code. Do I need to put an import statement there? You can also see in the image the file structure. The TextIO program is in a parallel folder to the one that PrintSquare is in. Do I need to somehow copy TextIO into some part of the PrintSquare folder? How? Where?

Regarding the stuff about the system path, I'm not sure I set one. Maybe. How can I check/set?

Thanks again!
Java-import-question.PNG
[Thumbnail for Java-import-question.PNG]
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Gordon wrote:I'm curious why you suggest a text editor rather than Netbeans. It seems like that or Eclipse has lots of useful tools for debugging, organizing, etc.


Indeed they do, but they also hide a lot of the basic business of compiling, running and jar-ing Java programs/apps from you; and it's that that you need to understand at the moment. Otherwise, you may never learn how to do it properly.

Nobody's saying don't ever use them - just don't use them yet.

Winston
 
Tim Gordon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just want to make sure that I'm not going back to the stone age (text editor) to learn how to program just because a few people were forced to learn that way back in the day. It's easy, in other words, as a more senior person to look back at the way kids are learning things these days and think they should really be going through the painful stuff that I (the expert) did in order to pay their dues. I'm not asserting that that is what is being done here. I'm merely saying that I know that that dynamic occurs in some contexts, and it could happen here. If it is a fairly widely held belief that learning via text editor is the way to go, I'll do it.

Text editor recommendations?

Java book recommendations?

Thanks!
 
Campbell Ritchie
Marshal
Posts: 80613
467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody has been bullshitting you, haven't they?
The reason for not recommending IDEs at this stage is (also) because of their steep learning curve. You need all the brainpower you can find to learn programming and you haven't got any to spare for learning the IDE.
There are recommendations about text editors in the link I gave you earlier. Also some suggestions about text editor options to enable to make things easier.

I can't read the PrintSquare program at the moment. You ought not to put it in a Screenshot but as text. Then we can copy it and try it ourselves. I suspect you are trying to do too much at once. You should compile the code after every 5 lines (approx), because that makes it much easier to find mistakes. The mistake might not occur where the compiler finds the error. I suggest you go as far as an empty class body and see whether there are any compiler errors. Remember you can inactivate any code by putting // at the start of the line. That is called commenting‑out. And reactivate it by remoiving the //
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Gordon wrote:Text editor recommendations?


Notepad+ (or whatever it's called) works for me.

Java book recommendations?


If you're a beginner: Head First Java; if you're a bit more experienced Effective Java. Otherwise, check out our book list.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic