473,657 Members | 2,595 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 6719
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.Prin tln( 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
3990
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 ID=MyID
3
1921
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 init(obj): obj.do = A.do
9
1223
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 { // alternatively we could name it public string Trim() {
6
6683
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 object just to get its type. I would appreciate if any one can can show me how to do this in vb.net. Thanks. Raghu/..
4
1296
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 Call AnotherClassLibrary.AnotherClass.UserLogin(userId) I do not understand how this code can call this function without first creating an instance of the AnotherClass.
22
2753
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 fun() {
10
3461
by: psbasha | last post by:
HI, Is it possible to access the Methods of a class without creating object? Thanks PSB
1
4256
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 Java, one can only call method by Class name if it is static method. In C++, you can call any function without creating an Object like -
3
1836
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
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.