File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static methods" Watch "Static methods" New topic

Static methods

Struggling Beginner

Joined: Jul 10, 2010
Posts: 2
I am very new to Java and am having a difficult time grasping the concepts being taught in my class. I think what keeps throwing me off is that the more I research the more I find that people do things differently. There are so many ways to do things how do I know if I am doing them right?

I have an assignment due this week in which we make changes to our existing application. The application has two classes. In one of the classes there is an array of items. I need to create a static mehtod in the other class that totals the value of the elements in the array.

Am I on the right track with the following code?

//Caluclate order total and return value
public static float orderTotal

//Create for loop
for(int i = 0; i <items.length (); i++)
sum +=myArray [i];

If I am on the right track how does the store items class know what the items arrray is since it was specified in the cash register class?
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 277

If the items array is public, then the other class can directly access the array. So if the array is called "items" and the class it resides in is called "MyClass", you would access the array by calling "MyClass.items"

pete stein

Joined: Feb 23, 2007
Posts: 1561
Cross-post: creating-static-methods.html

Please read: BeForthrightWhenCrossPostingToOtherSites
Struggling Beginner

Joined: Jul 10, 2010
Posts: 2

Again, I am sorry. I did not realize that it was rude of me to post the same message on two different sites. Know I know for next time.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

"Struggling Beginner", please check your private messages for an important administrative matter. Thanks.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Static methods
It's not a secret anymore!