Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:50:30+00:00 2026-06-09T06:50:30+00:00

How to avoid B Class object creation from outside world, and allow through only

  • 0

How to avoid B Class object creation from outside world, and allow through only A Class ?

I have 2 classes

public class A {
    B obj = null;
    public A() {
        obj = new B();
    }        
    public void methodA () {
        obj.methodB();
    }        
    // other methods
}

public class B {
    public void methodB () {
        // some logic            
    }
    //other methods
}

public class Client {
    public static void main (String s[]) {
        // Valid Call            
        A obj = new A();
        obj.methodA();  // Since methodB is called internally

        // Invalid Call , How to restrict this B object creation here ?
        B objB = new B();
        objB.methodB();

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:50:32+00:00Added an answer on June 9, 2026 at 6:50 am

    Here is solution, its working ..

    public class A {
    
        public static boolean isFromAClass = false;
        B obj = null;
    
        public A() {
            try {
                isFromAClass = true;
                obj = new B();
                isFromAClass = false;
            } catch (Exception e) {
            }
        }
    
        public void methodA() {
            System.out.println("methodA");
            obj.methodB();
        }
        // other methods
    }
    
    public class B {
    
        public B () throws Exception {
            if(!A.isFromAClass) {
                throw new Exception();
            }
        }
        public void methodB() {
            System.out.println("methodB");
            // some logic
        }
        //other methods
    }
    
    public class Client {
    
        public static void main(String s[]) throws Exception {
            // Valid Call
            A obj = new A();
            obj.methodA();  // Since methodB is called internally
    
            // Invalid Call , How to restrict this B object creation here ?
            B objB = new B(); // this line throws Exception
            objB.methodB();
    
        }
    }
    

    A, B, Client class may be in any package.

    using public static boolean isFromAClass = false; field, it is achieved.

    Only I need to change, instead od generalized Exception, it should be customized Exception as OutSideAClassBClassInstanceShouldNotBeCreated

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two classes, one inherits from the other Class A{ void
I have the following situation: public abstract class A { private Object superMember; public
I have an Object base class, and I have several derived classes called Item,
I have a base class and several concrete classes that derive from it. Lets
I have a templated class that I want to avoid copying (because of the
I have a code using protected field in super class. How can I avoid
I have to register an object in a container upon its creation. Without smart
My class contains an object from Interop and calls a method on it which
I have one class named A and one class B public class A :
Suppose I have, class example(object) def __init__(var1 = 10, var2 = 15, var3 =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.