Discussion:
Non-Static Variables and Static Methods...
(too old to reply)
TheGunslinger
2010-11-05 19:10:20 UTC
Permalink
First off, FYI, I have a fully functional program that I would like to
change one aspect.

I have several NON-static variables in my Class.

int myObjectCounter;
double myObjectDollars;
int myObjectTransactions;
String myObjectNames;
double myObjectCurrency;

I need to use them in a static method:

public static void myObjectDisplay(){

System.out.println("my text" + myObjectNames + "\t" + myObjectDollars
+ "\t" + myObjectCurrency + "\t" + myObjectTransactions);

} //End myObjectDisplay()


I want to call it from main();

The instantiation is of type: Object Array...

So, I want to use a For-Loop as follows to display the results for
each array object.

for(int i=1;i<arrayLength;i++){
myCurrencyObject[i].myObjectDisplay();
}

Suggestions?

TIA,

MJR
Wanja Gayk
2010-11-07 02:52:39 UTC
Permalink
TheGunslinger said...
Post by TheGunslinger
First off, FYI, I have a fully functional program that I would like to
change one aspect.
I have several NON-static variables in my Class.
int myObjectCounter;
double myObjectDollars;
int myObjectTransactions;
String myObjectNames;
double myObjectCurrency;
[..]
Post by TheGunslinger
I want to call it from main();
Suggestions?
In your static method create an object of that class and talk with that
one.

Kind regards.
--
"Gewisse Schriftsteller sagen von ihren Werken immer: 'Mein Buch, mein
Kommentar, meine Geschichte'. [..] Es wäre besser, wenn sie sagten:
'unser Buch, unser Kommentar, unsere Geschichte'; wenn man bedenkt, dass
das Gute darin mehr von anderen ist als von ihnen." [Blaise Pascal]

--- news://freenews.netfront.net/ - complaints: ***@netfront.net ---
TheGunslinger
2010-11-07 06:43:23 UTC
Permalink
Post by Wanja Gayk
TheGunslinger said...
Post by TheGunslinger
First off, FYI, I have a fully functional program that I would like to
change one aspect.
I have several NON-static variables in my Class.
int myObjectCounter;
double myObjectDollars;
int myObjectTransactions;
String myObjectNames;
double myObjectCurrency;
[..]
Post by TheGunslinger
I want to call it from main();
Suggestions?
In your static method create an object of that class and talk with that
one.
Kind regards.
Let's see if I understand you?

public static void main(String [] args){

Class [] myObject;
for(int i; i < arrayLength; i++){
myObject[i] = new classObject(arg1,arg2,arg3) ;
} //End For

myObject.method1();
myObject.method2();
myObject.method3();
etc...

Choose to Display Method (Y/N){
choose; } //end choosing
for(int i =1; i<arrayLength; i++){
myObject[i].displayNONStaticValues();
}//End For


}//End main()

-------------------------------------

public class Class{

//NON-static Object Associated Variable
int objectVar1
String objectVar2;
double objectVar3;

.........................

public static void displayNONStaticValues(){
System.out.println(objectVar1+"\t"+objectVar2+"\t"+objectVar3);
}//End displayNONStaticValues()

}//End class Class


As above does work, and is how I was forced to implement. BUT not what
I wanted.

I wanted all that code within main() to be in the static method so I
could just call within main() one method alone??

public static void displayNONStaticValues(){
Choose to Display Method (Y/N){
choose; } //end choosing
for(int i =1; i<arrayLength; i++){
System.out.println(objectVar1+"\t"+objectVar2+"\t"+objectVar3);
}//End For

Can it be done this way?

Ideally, I want only method calls inside main()?

TIA,

MJR
Rex
2010-11-09 03:43:24 UTC
Permalink
Post by TheGunslinger
Can it be done this way?
Ideally, I want only method calls inside main()?
TIA,
MJR
A static method can only refer to other static methods/fields directly,
for all other cases it will have to instantiate the relevant object first.


-------------------------------------
"Bother," said Pooh, as he mistook the liquefied stool sample for his cup
of coffee.
-------------------------------------

Loading...