简体   繁体   中英

Access variable from inner class without making it final

how could i get this to work:

public void onStart()
        {
            super.onStart();

            int dayN = 0;
            int i = 0;
            String day = null;
            String addFach;

            String mo1 = null;
            String mo2 = null;
            String mo3 = null;
            String mo4 = null;
            String mo5 = null;
            String mo6 = null;
            String mo7 = null;
            String mo8 = null;
            String mo9 = null;
            String mo10 = null;
            String mo11 = null;

            String di1 = null;
            String di2 = null;
            String di3 = null;
            String di4 = null;
            String di5 = null;
            String di6 = null;
            String di7 = null;
            String di8 = null;
            String di9 = null;
            String di10 = null;
            String di11 = null;

            String mi1 = null;
            String mi2 = null;
            String mi3 = null;
            String mi4 = null;
            String mi5 = null;
            String mi6 = null;
            String mi7 = null;
            String mi8 = null;
            String mi9 = null;
            String mi10 = null;
            String mi11 = null;

            String do1 = null;
            String do2 = null;
            String do3 = null;
            String do4 = null;
            String do5 = null;
            String do6 = null;
            String do7 = null;
            String do8 = null;
            String do9 = null;
            String do10 = null;
            String do11 = null;

            String fr1 = null;
            String fr2 = null;
            String fr3 = null;
            String fr4 = null;
            String fr5 = null;
            String fr6 = null;
            String fr7 = null;
            String fr8 = null;
            String fr9 = null;
            String fr10 = null;
            String fr11 = null;

            while (dayN <= 4)
            {
                if (dayN == 0)
                {
                    day = "Montag";
                } else if (dayN == 1)
                {
                    day = "Dienstag";
                } else if (dayN == 2)
                {
                    day = "Mittwoch";
                } else if (dayN == 3)
                {
                    day = "Donnerstag";
                } else if (dayN == 4)
                {
                    day = "Freitag";
                }

                while (i <= 10)
                {

                    BufferedReader buf;
                    try
                    {
                        buf = new BufferedReader(new FileReader("/sdcard/" + i + day + "fach.txt"));
                        addFach = buf.readLine();

                        if (day == "Montag")
                        {
                            if (i == 0) mo1 = addFach;
                            else if (i == 1) mo2 = addFach;
                            else if (i == 2) mo3 = addFach;
                            else if (i == 3) mo4 = addFach;
                            else if (i == 4) mo5 = addFach;
                            else if (i == 5) mo6 = addFach;
                            else if (i == 6) mo7 = addFach;
                            else if (i == 7) mo8 = addFach;
                            else if (i == 8) mo9 = addFach;
                            else if (i == 9) mo10 = addFach;
                            else if (i == 10) mo11 = addFach;
                        } else if (day == "Dienstag")
                        {
                            if (i == 0) di1 = addFach;
                            else if (i == 1) di2 = addFach;
                            else if (i == 2) di3 = addFach;
                            else if (i == 3) di4 = addFach;
                            else if (i == 4) di5 = addFach;
                            else if (i == 5) di6 = addFach;
                            else if (i == 6) di7 = addFach;
                            else if (i == 7) di8 = addFach;
                            else if (i == 8) di9 = addFach;
                            else if (i == 9) di10 = addFach;
                            else if (i == 10) di11 = addFach;
                        } else if (day == "Mittwoch")
                        {
                            if (i == 0) mi1 = addFach;
                            else if (i == 1) mi2 = addFach;
                            else if (i == 2) mi3 = addFach;
                            else if (i == 3) mi4 = addFach;
                            else if (i == 4) mi5 = addFach;
                            else if (i == 5) mi6 = addFach;
                            else if (i == 6) mi7 = addFach;
                            else if (i == 7) mi8 = addFach;
                            else if (i == 8) mi9 = addFach;
                            else if (i == 9) mi10 = addFach;
                            else if (i == 10) mi11 = addFach;
                        } else if (day == "Donnerstag")
                        {
                            if (i == 0) do1 = addFach;
                            else if (i == 1) do2 = addFach;
                            else if (i == 2) do3 = addFach;
                            else if (i == 3) do4 = addFach;
                            else if (i == 4) do5 = addFach;
                            else if (i == 5) do6 = addFach;
                            else if (i == 6) do7 = addFach;
                            else if (i == 7) do8 = addFach;
                            else if (i == 8) do9 = addFach;
                            else if (i == 9) do10 = addFach;
                            else if (i == 10) do11 = addFach;
                        } else if (day == "Freitag")
                        {
                            if (i == 0) fr1 = addFach;
                            else if (i == 1) fr2 = addFach;
                            else if (i == 2) fr3 = addFach;
                            else if (i == 3) fr4 = addFach;
                            else if (i == 4) fr5 = addFach;
                            else if (i == 5) fr6 = addFach;
                            else if (i == 6) fr7 = addFach;
                            else if (i == 7) fr8 = addFach;
                            else if (i == 8) fr9 = addFach;
                            else if (i == 9) fr10 = addFach;
                            else if (i == 10) fr11 = addFach;
                        }
                    }
                    catch (Exception e)
                    {

                    }

                    i++;
                }

                i = 0;
                dayN++;
            }

            RadioButton rB1 = (RadioButton) getView().findViewById(R.id.stunde1);
            RadioButton rB2 = (RadioButton) getView().findViewById(R.id.stunde2);
            RadioButton rB3 = (RadioButton) getView().findViewById(R.id.stunde3);
            RadioButton rB4 = (RadioButton) getView().findViewById(R.id.stunde4);
            RadioButton rB5 = (RadioButton) getView().findViewById(R.id.stunde5);
            RadioButton rB6 = (RadioButton) getView().findViewById(R.id.stunde6);
            RadioButton rB7 = (RadioButton) getView().findViewById(R.id.stunde7);
            RadioButton rB8 = (RadioButton) getView().findViewById(R.id.stunde8);
            RadioButton rB9 = (RadioButton) getView().findViewById(R.id.stunde9);
            RadioButton rB10 = (RadioButton) getView().findViewById(R.id.stunde10);
            RadioButton rB11 = (RadioButton) getView().findViewById(R.id.stunde11);
            DatePicker dP = (DatePicker) getView().findViewById(R.id.datePicker);

            dP.init(dP.getYear(), dP.getMonth(), dP.getDayOfMonth(), new DatePicker.OnDateChangedListener()
            {
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {
                    Calendar cal = Calendar.getInstance();
                    cal.set(year, monthOfYear, dayOfMonth);
                    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

                    switch(dayOfWeek)
                    {
                        case Calendar.MONDAY:
                            rB1.setText(mo1);
                            break;
                    }

                }
            });
        }

it should set the text of some radiobuttons depending on the weekday of the chosen date with the datepicker. i cant set the strings final because they get assigned with a value 2 times.

No,Not possible.

JLS # chapter 8

Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.

Any local variable used but not declared in an inner class must be definitely assigned (§16) before the body of the inner class.

As a side note:Make use of Collections instead of declaring that many fields.

Here is how can you access them example from docs:

class WithDeepNesting {
    boolean toBe;
    WithDeepNesting(boolean b) { toBe = b; }

    class Nested {
        boolean theQuestion;
        class DeeplyNested {
            DeeplyNested(){
                theQuestion = toBe || !toBe;  // here you are able to access now.
            }
        }
    }
}

如果声明它们作为成员变量(一个方法以外,优选之前onCreate()为了可读性),他们将在整个访问Activity甚至内部类的的Activity

The usual approach is to convert them into member variables. However, in your case, you need to rethink your design.

You have way too much code duplication. You should refactor it to use a collection, at which point the inner class problem solves itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM