473,503 Members | 3,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Methods without creating an object?

2 New Member
Hi there, I am new to Java and the concept of requiring a class/object to do everything..

I am creating a menu based program and hence need to re-use segments of code. If I was writing in C, I would just create a method above the main program and then call that method each time I wanted to, for example, display a menu.

The program I'm writing has two main classes from which I create objects to store data (imagine a 2D array of data but have used two arrays of objects instead). But let's say I want to re-use code that does not necessarily relate to either of the classes/objects, it's just the menus, etc. Would I create a separate class/object for that purpose? I am struggling with this for some reason..

Lil help? Thanks in advance!

Nick
Dec 27 '08 #1
2 6709
Bassem
344 Contributor
High Nick,

You can write Methods (Functions in C) in java in the program class (above the main method), truly above or down.

But you have one restriction is to declare the method as static, which mean that you can invoke ( call ) it from the class contains it, not using a specific object.
Just like the Main method.
Try this:

public static int MyMethodName (int x)
{
return x;
}

static void Main (string[] args)
{
System.out.Println( MyMethodName (5) );
}
Thanks
Bassem
Dec 27 '08 #2
JosAH
11,448 Recognized Expert MVP
@nickburton
Well, there you are: there's a third class probably named Menus or MenuHandler or whatever. Stick your methods in an object of that class. Of course you can go for the static method variant but sooner or later that'll shoot you in the back. And besides: those static methods also have to belong to some class.

kind regards,

Jos
Dec 27 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
3984
by: mcmg | last post by:
Hi, I have an asp app that works fine on a windows xp machine but does not work on a windows 2000 server. I have the following code in my global.asa: <OBJECT RUNAT=Server SCOPE=SESSION...
3
1912
by: Gabriele *darkbard* Farina | last post by:
Hi, there is a way to add methods to an object dynamically? I need to do something like this. I remember python allowed this ... class A(object): def do(s, m): print m @staticmethod def...
9
1215
by: cody | last post by:
There should be a way to mark methods without sideeffects with a special Attribute so that the compiler can recognize them and is able to issue a warning: public class string { //...
6
6672
by: Raghu | last post by:
In C#, the typeof keyword can be used to get a type of the class. This does not require object to be created first. However O am not sure how do the same thing in vb. I don't want to create the...
4
1281
by: D | last post by:
I was reviewing these vb.net classes which deal with user logins, logouts, cookies etc etc. I noticed that in the Page_Load function of a user control the code calls into another library like so...
22
2714
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
10
3434
by: psbasha | last post by:
HI, Is it possible to access the Methods of a class without creating object? Thanks PSB
1
4249
by: tobycraftse | last post by:
in C++ you can call function / methods without creating an object of that Class? like the the following, it calls Update() + Draw() functions by Class name CInterfaceGroup + CContainer. In...
3
1829
by: Reckoner | last post by:
would it be possible to use one of an object's methods without initializing the object? In other words, if I have: class Test: def __init__(self): print 'init' def foo(self): print 'foo'
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.