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 4552384
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:46:50+00:00 2026-05-21T16:46:50+00:00

may be its too simple but I couldnt find the right way. In C++

  • 0

may be its too simple but I couldnt find the right way.

In C++ I can write initWithParameter: xxx to instantiate a class and then in the init set some instance variables given the value at init time.

In Java I don’t know how to do that. Currently I do the following:

public class SpecialScreen extends BASEScreen{
private static final int ACTIVITY_1 = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); //create the instance
    defineScreenType (ACTIVITY_1); //second call to set the instance variable
    presentOnScreen();
}

While in BASEScreen:

public class BASEScreen extends Activity {
private Integer activityCode; // which activity should I do?

@Override
public void onCreate(Bundle savedInstanceState) {  // the creation
    super.onCreate(savedInstanceState);
}

// the setting of the instance variable
public void defineScreenType(int screenID) {
    activityCode = screenID;

}

This can’t be the best way of doing it. How to do this better?

Thanks

ADDED to show the calling of the SpecialScreen within BASEScreen:

    @Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    Intent i;
    switch (item.getItemId()) {
    case OTHER_PAGE_ID:
        // 
        if (activityCode == ACTIVITY_1) {
            i = new Intent(this, SpecialScreen2.class);
            i.putExtra("Task", ACTIVITY_2);
            startActivityForResult(i, ACTIVITY_2);
            finish();

        } else {
            i = new Intent(this, SpecialScreen1.class);
            i.putExtra("Task", ACTIVITY_1);
            startActivityForResult(i, ACTIVITY_1);
            finish();
        }

        return true;

ps I know that putting the Extra is not required anymore. This was the way I did it before I had the two SpecialScreen subclasses and always called the BASEScreen with this parameter.

  • 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-05-21T16:46:51+00:00Added an answer on May 21, 2026 at 4:46 pm

    Correct, there is no “default” syntax like in c++. You have to do it in the constructor. Mind you, you don’t need to use the setter method, you could make activityCode protected rather than private, and just do:

    activityCode = ACTIVITY_1;
    

    The other option is using the Builder Pattern to construct your objects, using a set of defaults inside the builder that you override (when needed) when requesting the object be built.

    Edit in response to comments below:

    I apologize for some confusion as I was calling it a “constructor” when it’s not.

    If in BASEScreen you change the access to protected from private

    public class BASEScreen extends Activity {
        protected Integer activityCode;
    

    You can then access that in the SpecialScreen subclass:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityCode = 1; // Or ACTIVITY_1 if you'd like
        presentOnScreen();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

May be its a simple question but it's really driving me crazy. I just
Okay this may seem too simple of a question but I've wasted enough time
I'm trying to split a string on its punctuation, but the string may contain
I'm sorry if this is too simple for some people, but I still don't
I am trying to write a simple mail client in Java with sockets. But
Touch may specify self to get the touch location in its own coordinate system
There's a control with a png as its background. And it may be clicked
This may be silly, but it's been nagging the back of my brain for
This may seem a trivial question, but it's one that's bothered me a lot
This may be a ridiculous question, but it's been bothering me for a while.

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.