0

im not able to declare public class download inside my main class in android studiov4.2

public class MainActivity extends AppCompatActivity {
    //Intialistion of variables:
    ListView listView;
    ArrayList<String> arrayList = new ArrayList<String>();
    ArrayAdapter arrayAdapter;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    //declaration of variables :
    listView = findViewById(R.id.ListView);
    arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);  
    listView.setAdapter(arrayAdapter);


    public class Download extends AsyncTask<String,Void,String>{
    
        @Override
        protected String doInBackground(String... strings) {
            return null;
        }
      }
    }
  }



[Screenshot of error is attached see it for refrence][1]
  [1]: https://i.stack.imgur.com/DbqDY.png

Error - Modifier 'public' not allowed here

  • 1
    which is normal. a Java file can only contain one (non static) public class – Stultuske Jun 21 '21 at 07:43
  • 1
    If you indent your code consistently, you'll see that you try to declare a class inside the body of the `onCreate()` method. And that doesn't make sense in Java. I'd recommend that you read some material about the overall structure of multi-class Java programs. – Ralf Kleberhoff Jun 21 '21 at 08:02
  • hi, but can you please clear why it's allowing me to declare class as default , when i declare same DOWNLOAD CLASS as default class (access specifer) it is not throwing any errors. – KHUSHAL Tiwary Jun 21 '21 at 08:38

1 Answers1

0

Lol, error solved - I was creating public Class DownLoad inside OnCreate method , Reality is you just can't create a class inside a method that is already inside a class called "Main". aka - Methods can be created inside class, not wise-versa. Sorry for the silly mistake .