Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 24th, 2008, 11:54 PM
Registered User
 
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help needed regarding Java classes

Hi,
  I am facing problem with java classes.I have created an object p of Parameter type and declared it to be static.I have to call a non-static method on it p.add(getTickCount()).This method is imported from package uchicago.src.sim.engine .This is method from interface SimModel of this package .You can refer to this link to know more about the method I am talking about
http://repast.sourceforge.net/api/index.html
When I am compiling the program,it shows an compilation error
"non-static method cannot referenced by static object"
How am I supposed to refer a non-static method using static object.Is there any way I can store the result returned by getTicCount in static variable ?Please help!!!

Thank you
Sadia

 
Old February 29th, 2008, 01:40 PM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Well it will. Why are you declaring your object static?

As you haven't posted any code I wrote a working version of what I thought you might be trying to do:

Code:
public class dyn {
        public dyn() {
                System.out.println(getIt());
        }

        public static int getIt() {
                return 42;
        }
        public static void main(String argv[]) {
                dyn d=new dyn();
        }
}
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
 
Old February 11th, 2009, 08:49 AM
Registered User
 
Join Date: Feb 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is not recommended to call static method using no static object. use class name when you want to call static method. If your design force to do that it simple means there is problem in your design.





Similar Threads
Thread Thread Starter Forum Replies Last Post
tomcat, jsp, java classes Befekadu Apache Tomcat 3 April 2nd, 2009 07:30 AM
Java classes spudgun2005 J2EE 2 June 21st, 2005 03:24 AM
Using Java Classes from C# gettipu General .NET 0 February 7th, 2005 06:59 PM
Compile Java classes to EXE vb_programmer Java GUI 7 December 14th, 2004 08:16 AM
ASP support for classes written in Java... rdwash Classic ASP Components 1 July 18th, 2003 11:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.