Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods

 
Struggling Beginner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1561
 
Struggling Beginner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Struggling Beginner", please check your private messages for an important administrative matter. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic