• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reaching methods in the same class

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you have to instantiate the class to reach methods in the same class

as in:



i couldnt go:
MyClass.myTest;

i had to instantiate the MyClass then i could get to its methods...is there a way in the main to reach methods in the same class?

thanks

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only if they are static. But static methods aren't something to create lightly. A method should be static if, and only if, there's a very good reason for it to be so. And "easy to access" isn't one of those very good reasons.

 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fully bear,

thats why its better to have a separate file for each class isnt it...yes i was aware i could go static...but i do understand what static variable/methods are used for...sure this was only for testing,but you have pointed me out on how not to do things.

thanks
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:thats why its better to have a separate file for each class isnt it...


Umm, not really. That's a different concept and doesn't have much to do with static/non-static.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but doesnt it make sense that if your classes are separate (and you dont use a lot of sub classes) it will be easier to reach other classes methods etc without having to instantiate?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:but doesnt it make sense that if your classes are separate (and you dont use a lot of sub classes) it will be easier to reach other classes methods etc without having to instantiate?



Sounds like you want to do procedural programming with an object oriented language. Probably not a good idea, heck, it is a bad idea ... but I won't debate it ...

Yes, if you make all your methods static, and use classes as a way to separate / organize those methods, you can write code without instantiating a single custom object.

Henry
 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:do you have to instantiate the class to reach methods in the same class

as in:




You DO NOT need to instantiate the class  to call the method if your calling the method from within the same class the method is in.  if you wanted to call the myTest function all you  have to write in the main method is


 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Matty wrote:
You DO NOT need to instantiate the class  to call the method if your calling the method from within the same class the method is in.  if you wanted to call the myTest function all you  have to write in the main method is




Really? Have you tried compiling this code? ... BTW, welcome to the ranch.

Henry
 
Jesse Matty
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are correct it does not compile you can do what I showed with other classes and other methods but not the main method.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Matty wrote:But making methods static is a  bad idea.


Making broad, sweeping statements like that isn't exactly a great idea either. There are situations where making a method static and not dependent on a particular instance of a class is the most appropriate choice to make. The trick is to know how to recognize the right conditions in which to do so.
reply
    Bookmark Topic Watch Topic
  • New Topic