• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Problem with static variables.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 201
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic