wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Quick Help on Java syntax? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quick Help on Java syntax?" Watch "Quick Help on Java syntax?" New topic
Author

Quick Help on Java syntax?

Zach Shaner
Greenhorn

Joined: Oct 05, 2011
Posts: 6
hello JavaRanch!

This will be my first post on these forums as a registered user(I have been using these forums for a while but I decided to register a few days ago). I am fairly new to java and I had a quick question on how I would accomplish this task. I have a main .java file that contains a simple line of code located below:



That's a quick example I just made up. Lets pretend that this line compiled perfectly fine with no errors. Lets also pretend I have a separate java file named fruit basket and I wanted to use that apple instance with the data from the .getColor() and that retyping that code in the new class wasn't an option. How would I access that instance with the data stored in it from another java file?

If you need a better clarification don't hesitate to ask. The reason I didn't include my actual code was because I am using a separate API that not a lot of people know exist and I don't want to get caught up on that syntax.

Thanks in Advance,
-Zach
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Zach Shaner wrote: Lets also pretend I have a separate java file named fruit basket and I wanted to use that apple instance with the data from the .getColor()

I am having to guess at what "that apple instance with the data from the .getColor()" means. Unless getColor() returns an object that is compatible with assigning to an Apple variable, the line with getColor() will not compile.

and that retyping that code in the new class wasn't an option.


I do not know what "new class" you're referring to here.

How would I access that instance with the data stored in it from another java file?


And I don't know for sure any more what "that instance with the data stored in it" refers to either.

Zach Shaner
Greenhorn

Joined: Oct 05, 2011
Posts: 6
Ralph Cook wrote:
Zach Shaner wrote: Lets also pretend I have a separate java file named fruit basket and I wanted to use that apple instance with the data from the .getColor()

I am having to guess at what "that apple instance with the data from the .getColor()" means. Unless getColor() returns an object that is compatible with assigning to an Apple variable, the line with getColor() will not compile.

and that retyping that code in the new class wasn't an option.


I do not know what "new class" you're referring to here.

How would I access that instance with the data stored in it from another java file?


And I don't know for sure any more what "that instance with the data stored in it" refers to either.



Thanks for your reply! I'll address each of your problems in a 1.2.3 style format.

1)I guess it would make more sense if I used getQuantity(); instead of getColor(); As I said this is an example and the actual code I'm using works perfectly fine and does compile. By that apple instance I mean Apple apple.

2) By new class I meant a new Java File.

3) "that instance" again refers to "Apple apple" and the data comes from the "getQuantity();"


In essense all I really want to know is how to use an instance from another java file while maintaining that information in that instance.(if this doesnt make sense ignore this part. Refer back to the example)

-Zach
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Zach Shaner wrote:In essense all I really want to know is how to use an instance from another java file while maintaining that information in that instance.(if this doesnt make sense ignore this part. Refer back to the example)


Well, um, that isn't the right question. You can certainly create a new instance of, say, the Apple class. But it isn't your job to "maintain that information in that instance" -- at least not if that means anything at all. The Apple instance will be responsible for maintaining its own "state", i.e. the information normally contained in an Apple instance. You can call methods of the instance to get information from the Apple instance, or to pass information to the instance, but you can only call methods which are defined by the Apple class.

Was that what you were trying to ask? It sounds like you weren't there when objects and classes were discussed, or perhaps you haven't got to those topics yet. I would suggest reading this tutorial: Lesson: Classes and Objects.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Quick Help on Java syntax?
 
Similar Threads
MySQL tricks for managing test data
Interfaces
Log4j Appenders
How to pipe emails to a java file and read?
How to separate the design from event handling in swing