Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

non-static context

Thread view: 
ste1986 - 03 Jan 2006 18:05 GMT
i get the error message "non-static method getName() cannot be referenced
from a static context" what does this mean. i have a getName method in
another class called Volunteers.

if (Volunteers.getName().equals(name))
Mark Haase - 03 Jan 2006 18:25 GMT
In article
<1418e0844b762bcd1f9a50b6aeef6eea@localhost.talkaboutprogramming.com>,

> i get the error message "non-static method getName() cannot be referenced
> from a static context" what does this mean. i have a getName method in
> another class called Volunteers.
>
> if (Volunteers.getName().equals(name))

You can't call an instance method on a class.

Try instead:

Volunteers v = new Volunteers();

...

if (v.getName.equals(name))

|\/|  /|  |2  |<
mehaase(at)gmail(dot)com
ste1986 - 03 Jan 2006 19:03 GMT
where do i put this tho. in the class with the getName method?
Volunteers v = new Volunteers();
Ricky Clarkson - 04 Jan 2006 05:12 GMT
You might want to look at the following tutorial:

http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

Possibly others from the reallybigindex:
http://java.sun.com/docs/books/tutorial/reallybigindex.html

Cheers.
Ricky Clarkson - 04 Jan 2006 06:03 GMT
You might want to look at the following tutorial:

http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

Possibly others from the reallybigindex:
http://java.sun.com/docs/books/tutorial/reallybigindex.html

Cheers.
kavili - 04 Jan 2006 06:14 GMT
Hi

if (Volunteers.getName().equals(name))

I would like to explain 3 points in this context

1. The above line is giving error reason is your getName() is not
static
//you can call  variables and methods with the help of class name when
they are static.
2. You can refer a static member in a non static block/mehtod, but
reverse is not.
// to get this done you have to create the instance of the class and
call on it.
3. Static variables, methods will occupy the memory irrespective of
their usage.
   ie they will occupy memory at first access of them and will not be
garbage collected till the application terminated.
// use static key word if it's referred in multiple locations/classes
in your application.

hope it clarifies your doubt.

--suresh

> i get the error message "non-static method getName() cannot be referenced
> from a static context" what does this mean. i have a getName method in
> another class called Volunteers.
>
> if (Volunteers.getName().equals(name))
David  Wahler - 04 Jan 2006 13:11 GMT
> i get the error message "non-static method getName() cannot be referenced
> from a static context" what does this mean. i have a getName method in
> another class called Volunteers.
>
> if (Volunteers.getName().equals(name))

The expression "Volunteers.getName()" is like asking "how many pages
are there in a book?" It doesn't make sense without referring to a
specific object.

-- David


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.