Results 1 to 3 of 3
Thread: Controlling method calls
- 01-04-2008, 12:52 PM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 111
- Rep Power
- 0
Controlling method calls
I wrote a Java class with two constructors and few methods:
Java 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 { ... } }
Thanks for your help.
- 01-04-2008, 01:49 PM #2
Try using a flag
Hello bugger.
You can add a private flag attribute to monitor which constructor was called. For example:
Java 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 } } }
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-04-2008, 02:14 PM #3
Senior Member
- Join Date
- Nov 2007
- Posts
- 111
- Rep Power
- 0
Similar Threads
-
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 06:12 PM -
Method Help
By pringle in forum New To JavaReplies: 4Last Post: 04-16-2008, 02:23 PM -
controlling GC
By ravian in forum EclipseReplies: 2Last Post: 01-03-2008, 09:13 AM -
how to have function variables remember their values between calls
By asterik123 in forum New To JavaReplies: 2Last Post: 08-03-2007, 05:06 PM -
How to access system calls in java
By Albert in forum New To JavaReplies: 1Last Post: 07-13-2007, 04:12 PM
Bookmarks