GeeCON Prague 2014*
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
Author

Static methods

Struggling Beginner
Greenhorn

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: 273

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"


SCJP 6 || SCWCD 5
pete stein
Bartender

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

Please read: BeForthrightWhenCrossPostingToOtherSites
Struggling Beginner
Greenhorn

Joined: Jul 10, 2010
Posts: 2
Pete,

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
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
GeeCON Prague 2014
 
subject: Static methods