• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Question regarding static

 
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ello,

Sorry, I just found the solution to my original question; I have a second question regarding the static keyword



Will my functions always have to be declared as static inside this class to work with main?
 
author & internet detective
Posts: 41184
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Travis Roberts wrote:Will my functions always have to be declared as static inside this class to work with main?


Yes, unless you create an instance of the object. For example, suppose you write One one = new One(); one.myMethod().

Then myMethod() doesn't need to be static.
 
Greenhorn
Posts: 10
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Travis Roberts wrote:Will my functions always have to be declared as static inside this class



No,you can create non-static method in your class.Then create object of this class in main method,So that you can use this newly created object to call non-static method from main.
ex. objectreferencevaraible.YourMethod();
 
Marshal
Posts: 76107
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Irfan Jamadar, welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 76107
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is often a serious mistake to mark things static “because it seems to work like that” or to correct a compiler error. Please search this forum and the Java in General forum, because there have been several discussions about static and not static. I am not sure when, maybe about 1 month ago.
 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though your question is already answered that you need to create an object of the class, in order to avoid static.
I am curious to know, what you are trying to do with , this code
 
reply
    Bookmark Topic Watch Topic
  • New Topic