• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with static variables.  RSS feed

 
Bill Jillians
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to prove a Mathematical law called the Hailstone sequence. I'm using BlueJ to work in. I'm doing all the work in a main program in which the mathematical work is done and all the variables are defined as public static outside the main routine. Usually BlueJ gives me a list of the functions available and I can run main from there. I've been having trouble with static variables since my last project. I'm not sure I understand fully what is involved. Anyway, the following code compiles but I cannot access the main program in BlueJ to run it. Any suggestions as to what I'm doing wrong.



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Why are you declaring all those variables as static member variables of the class? Why not just as local variables inside the main method?

Most of the time it's a good idea to limit the scope of variables as much as possible. If there's not really a reason why they should be static member variables, they should just be local variables.

I don't know BlueJ, but probably the reason why you can't run this is because your main method is private. Make it public instead.
 
Gaurav Varma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use - public static void main ... instead?
I see the main method is private
 
Bill Jillians
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I've got it working now. The problem was the access for main. It should be public .. not private. Thanks for all your help.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Jillians wrote:...and all the variables are defined as public static outside the main routine...

As Jesper said: usually a bad idea. The only use I've ever found for a static variable is for generating sequence numbers, although there may be others; otherwise mine are always final (ie, named constants).

Winston
 
Bill Johnston
Ranch Hand
Posts: 201
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Jillians wrote: ... I'm doing all the work in a main program in which the mathematical work is done and all the variables are defined as public static outside the main routine. ... I've been having trouble with static variables since my last project. I'm not sure I understand fully what is involved.


Bill,

You are doing all your work in "the main method" of your class file "series", not in a "main program". I'm not nit picking - there's a meaningful difference; especially since I think you said you were coming from a Fortran background, which is traditionally non OOP.

About the "static" concept. Tell us what trouble you are having. In Java, static means that you can define a method or member (what your calling a variable) to be used without needing an enclosing Object (the first "O" in OOPs). While this in not what static is supposed to be used for, for the most part, it means that you can fundamentally code the OOPs language Java in a procedural way, and for all intents and purposes this is what you are doing here. So, you can code this way, but in rather most instances you shouldn't be. Why use Java for that when you can use Fortran?

Please post more specific questions along this line if you have them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!