• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question regarding static  RSS feed

 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • 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?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
irrfan jamadar
Greenhorn
Posts: 10
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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();
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Irfan Jamadar, welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • 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.
 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!