Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-04-2008, 01:52 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Controlling method calls
I wrote a Java class with two constructors and few methods:

Code:
public class A { public A() // default constructor { ... } public A(in a) // constructor with one parameter { ... } public void processSelectedData() // should be called only when object of class A is created // using constructor with one parameter { ... } public void processEverything() // should be called only when object of class A is // created using default constructor { ... } }
The code comments will brief you about what I am looking for. How can I impose these rules. I don't want the user to call the every method with an object.

Thanks for your help.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-04-2008, 02:49 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Try using a flag
Hello bugger.

You can add a private flag attribute to monitor which constructor was called. For example:
Code:
public class A{\ // flag types private enum TConstructor {noParameter, oneParameter}; // the flag TConstructor constructorUsed; // default constructor public A(){ constructorUsed = TConstructor.noParameter; } // constructor with one parameter public A(in a){ constructorUsed = TConstructor.oneParameter; } // should be called only when object of class A is created // using constructor with one parameter public void processSelectedData() throws Exception{ if (constructorUsed != TConstructor.oneParameter){ throw new Exception("processSelectedData() should be called only when object of class A is created using constructor with one parameter."); } else { // TODO: Add your code here } } // should be called only when object of class A is // created using default constructor public void processEverything() throws Exception{ if (constructorUsed != TConstructor.noParameter){ throw new Exception("processEverything() should be called only when object of class A is created using default constructor."); } else { // TODO: Add your code here } } }
This code was checked by hand. Hope this helped you.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-04-2008, 03:14 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Great. Thanks a dozen.
This is what I was looking for.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method Help pringle New To Java 4 04-16-2008 03:23 PM
controlling GC ravian Eclipse 2 01-03-2008 10:13 AM
how to have function variables remember their values between calls asterik123 New To Java 2 08-03-2007 06:06 PM
How to access system calls in java Albert New To Java 1 07-13-2007 05:12 PM
method not abstract, does not override actionperformed method. Theman New To Java 1 05-08-2007 08:13 AM


All times are GMT +3. The time now is 01:21 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org