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

  • Home
  • SEARCH
  • 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 6061193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:54:38+00:00 2026-05-23T08:54:38+00:00

I need to create a global Arraylist consisting Instances of my custom class ‘Book’

  • 0

I need to create a global Arraylist consisting Instances of my custom class ‘Book’ to be used across the application.

So I created a file ‘MyBooks.java’ containing ‘MyBooks’ class (extending application class) in which custom class ‘Book’ and Arraylist ‘Booklist’ are defined.

In the Main Activity file ‘ExampledroidActivity.java’ I need to load data from server, create ‘Book’ instances and add these instances to the ArrayList ‘BookList’.

the problem is that it doesn’t seem to recognize Book class in file ‘ExampledroidActivity.java’.

Can Someone plz look at the code and point out what i am doing wrong.

Code from both files is given below

MyBooks.java –

package com.dubloo.exampledroid;

import java.util.ArrayList;
import android.app.Application;

public class MyBooks extends Application {

    public class Book {

        public int No;
        public String Name;
        public String Author;
        public boolean IsAvailable;

     //  constructor
        public Book(int bookNo, String bookName, String bookAuthor, boolean bookIsAvailable) {
                    No = bookNo;
                    Name = bookName;
                    Author = bookAuthor;
                    IsAvailable = bookIsAvailable;
        }
    }

    public ArrayList<Book> BookList  = new ArrayList<Book>();

}

ExampledroidActivity.java –

package com.dubloo.exampledroid;

import android.app.Activity;
import android.os.Bundle;


public class ExampledroidActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyBooks someBooks = ((MyBooks)getApplicationContext());

//here I get data from a server 
//for this example generating dummy data

        for(int n=1; n<20; n++){
            String Name = "The Book" + n;
            String Author = "Blah " + n;
            boolean IsAvailable = true;

//declare here an instance of class Book - in this part book class is not recognized
//definately doing some thing wrong here

            someBooks.Book thisBook = new someBooks.Book(n, Name, Author, IsAvailable);

//add book to the Booklist Arraylist

            someBooks.BookList.add(thisBook);
        }        


}

}

If its a noob question plz bear with me,
Thanks in advance,

Shobhit

  • 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-23T08:54:39+00:00Added an answer on May 23, 2026 at 8:54 am

    A better answer could be to

    • separate you class book in another file (Book.java)
    • make the BookList private and provide accessors (setter / getter )
    • respect java naming conventions : class start by upperCase letter then camel case, variables start with lowerCase letter then camel case.
    • make your android application class a singleton.

    There are some flaws in your design so far, but go on learning, Android is fun.

    Regards,
    Stéphane

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

Sidebar

Related Questions

I've created a global handler for ajaxSuccess , but I need to be able
I need create custom dialog and put JPanel into it. Is it possible?
Need to create a custom DNS name server using C which will check against
I need in global list in my gtk+ application, i use for it GList:
I'm trying to create a global Robot variable in a Java class without throwing
I need to create some global android variables. Some posts here suggested me to
Using boost python I need create nested namespace. Assume I have following cpp class
I need to create a simple c# application to add some quarterly figures. I
i need create an email list sending to many emails. what is best solution
I need to create a historical timeline starting from 1600's to the present day.

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.