Results 1 to 3 of 3
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default 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
    {
    ...
    }
    
    }
    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.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default 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
            }
        }
    }
    This code was checked by hand. Hope this helped you. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. Method Help
    By pringle in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 01:23 PM
  3. controlling GC
    By ravian in forum Eclipse
    Replies: 2
    Last Post: 01-03-2008, 08:13 AM
  4. Replies: 2
    Last Post: 08-03-2007, 04:06 PM
  5. How to access system calls in java
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 03:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •