473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question about static methods

Given this very basic program:

public class Blah {

public static void main (String [] args) {

doStuff(); //Calls the method doStuff

}//Closes main

public static void doStuff(); {
severalCommands;
} //Closes doStuff

} //Closes public class blah

I'd like to be able to not have to make the method doStuff() static and
would like to simply call it "pubic void doStuff" but I get an error message
telling me I can not call a non-static class from a static method (since
main is static). Is there a way to get around this so that doStuff() can
still be non-static?

Thanks.

-AckbarJedi


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #1
4 4198
"AckbarJedi" <Ac***************@hotSPAMmail.com> wrote in message
news:40********@corp.newsgroups.com...
Given this very basic program:

public class Blah {

public static void main (String [] args) {

doStuff(); //Calls the method doStuff

}//Closes main

public static void doStuff(); {
severalCommands;
} //Closes doStuff

} //Closes public class blah

I'd like to be able to not have to make the method doStuff() static and
would like to simply call it "pubic void doStuff" but I get an error message telling me I can not call a non-static class from a static method (since
main is static). Is there a way to get around this so that doStuff() can
still be non-static?

Thanks.

-AckbarJedi


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


You need to instantiate Blah and use your instance of blah to call the
instance method.

Which means...

Blah jedi = new Blah();
jedi.doStuff();

.... goes in your main method

and doStuff() becomes an instance method (get rid of 'static').

John
Jul 17 '05 #2

"AckbarJedi" <Ac***************@hotSPAMmail.com> wrote in message
news:40********@corp.newsgroups.com...
Given this very basic program:
<SNIP>
I'd like to be able to not have to make the method
doStuff() static and would like to simply call it "pubic
void doStuff" but I get an error message telling me I can
not call a non-static class from a static method (since
main is static). Is there a way to get around this so that
doStuff() can still be non-static?


The following will do the trick:

public class Blah
{
public static void main (String [] args)
{
Blah blah1 = new Blah(2), blah2 = new Blah(6);
blah1.doStuff();
blah2.doStuff();
}

public Blah(int number) { this.number = number; }

public void doStuff()
{
// ...
System.out.println(number);
// ...
}

private int number;
}

I strongly urge you to complete the tutorials at the Sun Java site:

http://java.sun.com/docs/books/tutorial/index.html

I hope this helps.

Anthony Borla
Jul 17 '05 #3
AckbarJedi wrote:
Given this very basic program:

public class Blah {

public static void main (String [] args) {

doStuff(); //Calls the method doStuff

}//Closes main

public static void doStuff(); {
severalCommands;
} //Closes doStuff

} //Closes public class blah

I'd like to be able to not have to make the method doStuff() static and
would like to simply call it "pubic void doStuff"


I'm sure you've been given good advice but it might also help if you
called the method "public void doStuff()"!

Jul 17 '05 #4
"John" <?> wrote in message
news:40*********************@lovejoy.zen.co.uk...
"AckbarJedi" <Ac***************@hotSPAMmail.com> wrote in message
news:40********@corp.newsgroups.com...
Given this very basic program:

public class Blah {

public static void main (String [] args) {

doStuff(); //Calls the method doStuff

}//Closes main

public static void doStuff(); {
severalCommands;
} //Closes doStuff

} //Closes public class blah

I'd like to be able to not have to make the method doStuff() static and
would like to simply call it "pubic void doStuff" but I get an error

message
telling me I can not call a non-static class from a static method (since
main is static). Is there a way to get around this so that doStuff() can still be non-static?

Thanks.

-AckbarJedi


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


You need to instantiate Blah and use your instance of blah to call the
instance method.

Which means...

Blah jedi = new Blah();
jedi.doStuff();

... goes in your main method

and doStuff() becomes an instance method (get rid of 'static').

John

Thanks. That's extremely helpful and solves the problem elegantly!

--
AckbarJedi,
---
"That theory is worthless. It isn't even wrong!" ~Wolfgang Pauli
---
To e-mail remove DeathtoSpam from my e-mail address.


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Tom Dacon | last post by:
I'm curious to see if anyone has an opinion on this little design question - I'm doing a computational astronomy library in C#, purely for my own use, and one of the things that happens regularly...
5
by: allison | last post by:
Can someone point me to a guide on when to use static methods versus instance methods in a class? For instance, is it bad design to have a static method that creates an instance of another class? ...
3
by: Amadelle | last post by:
Hi All and thanks in advance, I wanted to know when is a good idea to use a static class (with static constructor) and when to use instance classes? I have read couple of articles on line and...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
11
by: dhnriverside | last post by:
Hi peeps Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't written any windows apps for years, let alone dos apps (been web programming) and I've hit a dumb error... ...
4
by: Joe Fallon | last post by:
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when...
6
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
5
by: wrecker | last post by:
Hi all, I have a few common methods that I need to use at different points in my web application. I'm wondering where the best place would be to put these? I think that I have three options. ...
15
by: dn | last post by:
I'm starting an n-tier application with an ASP.NET 2.0 presentation layer, a business layer, a data access layer, and a SQL Server 2005 database, and I have a question. In the business and data...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.