• 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Quick Help on Java syntax?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 28536
114
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic