inner-classes

About local final variables in Java

依然范特西╮ 提交于 2019-12-03 21:57:05
In java Program, parameters which is defined as String in method declaration. But in method definition it is accessed as final String variable. Whether it'll lead to some issues (like security, memory problem)? For Example: Method Declaration join(String a,String b); Method definition public void join(final String a,final String b) { Authenticator au = new Authenticator(){ public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(a,b)} }; } Please help for me and clarify my doubts. Thanks in advance P.S. I'm accessing a and b as final variable because I've

newInstance() with inner classes

纵然是瞬间 提交于 2019-12-03 18:03:29
问题 I've been working on an instantiation method that will allow me to package a variety of similar classes into one outer class. I could then instantiate each unique class type by passing the name of that type to the constructor. After a lot of research and errors, this is what I have come up with. I have left an error, to demonstrate my question. import java.lang.reflect.Constructor; public class NewTest { public static void main(String[] args) { try { Class toRun = Class.forName("NewTest$" +

Android Weak Reference of Inner Class

别来无恙 提交于 2019-12-03 16:27:31
I have gone through the article http://developer.android.com/resources/articles/avoiding-memory-leaks.html . In this article it is suggested to use static inner class with Weak Reference . public class GalleryVideo extends Activity { private int AUDIO_NO = 1; ........................... ................ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); gallery = (Gallery) findViewById(R.id.examplegallery); gallery.setAdapter(new AddImgAdp(this)); } static public class AddImgAdp extends BaseAdapter { private int GalItemBg; private Context cont;

No enclosing instance of type PerfHelper is available due to some intermediate constructor invocation

半城伤御伤魂 提交于 2019-12-03 16:12:59
问题 Consider the below code: class abstract Normal1 extends Something { } class Outer { class abstract Inner extends Normal1 { } } class General extends Outer.Inner // Problem occurs at this { } The error I am getting is "No enclosing instance of type PerfHelper is available due to some intermediate constructor invocation" My question is can I extend the inner class like above ? 回答1: Declare the inner class as static and you should be able to extend it: class outer { static abstract class inner

local variable is accessed within inner class (java)

牧云@^-^@ 提交于 2019-12-03 15:22:45
问题 I got two errors after I compiled my code. The errors are: 1. local variable input is accessed within inner class; needs to be declared final String name = input.getText(); 2. local variable c_age is accessed within inner class; needs to be declared final Object child_age = c_age.getSelectedItem(); This is my code: import javax.swing.*; import java.awt.event.*; public class GUI { public static void main(String[] args) { JFrame frame = new JFrame("Try GUI"); JLabel l1 = new JLabel("Please

Where to put inner classes? [closed]

♀尐吖头ヾ 提交于 2019-12-03 14:25:32
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Some might like to argue that this is a candidate for the least important issue of all times. Yet code style is a very important topic

Inner static class inside inner class cannot be converted

我是研究僧i 提交于 2019-12-03 12:45:01
Inspired in this question: How to implements Iterable I decided to make a basic linked list implementation and implement an iterator in order to have a code like this: MyList<String> myList = new MyList<String>(); myList.add("hello"); myList.add("world"); for(String s : myList) { System.out.println(s); } The code wasn't hard to deal with, creating a class MyList<T> implements Iterable<T> with a private static class Node<T> and a private class MyListIterator<T> implements Iterator<T> , but then I came across a problem when implementing my own version of Iterator#remove : class MyList<T>

Outer vs. Super class

[亡魂溺海] 提交于 2019-12-03 11:42:42
问题 Does super has higher priority than outer class? Consider we have three classes: ClassA ClassB Anonymous class in ClassB that extends ClassA ClassA.java: public class ClassA { protected String var = "A Var"; public void foo() { System.out.println("A foo()"); } } ClassB.java: public class ClassB { private String var = "B Var"; public void test() { new ClassA() { public void test() { foo(); System.out.println(var); } }.test(); } public void foo() { System.out.println("B foo()"); } } When I call

Constructor reference for inner class fails with VerifyError at runtime

廉价感情. 提交于 2019-12-03 11:33:21
问题 I am creating a supplier for an inner class constructor using the lambda ctx -> new SpectatorSwitcher(ctx) . IntelliJ suggested that I change it to SpectatorSwitcher::new instead. SpectatorSwitcher is a non-static inner class of the class I'm working in. The suggested code compiles fine (using maven) but I get the following VerifyError on execution: Exception in thread "main" java.lang.VerifyError: Bad type on operand stack Exception Details: Location: Test.lambda$runTest$8(LTest$Worker;)V @2

Referencing non-final variable: why does this code compile?

为君一笑 提交于 2019-12-03 09:29:49
First off, I apologise if this is a duplicate question. I found many similar ones, but none that directly address my question. In preparation for an upcoming exam, I am doing a past paper. It has a question that gives a code snippet. We have to state if it compiles, and if not, write the line at which the first compiler error occurs and explain it. This is the snippet: public static void main(String[] args) { JFrame f = new JFrame("hi"); JTextField jtf = new JTextField(50); jtf.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent evt) { jtf.setText(evt