Skip to main content
Filter by
Sorted by
Tagged with
16 votes
3 answers
913 views

I have a function template f, defining in its body a local class A with another nested class B. Both classes are not templates. Must I name the inner class as typename A::B or shorter variant A::B is ...
Fedor's user avatar
  • 24.7k
Advice
2 votes
4 replies
249 views

Any C++ class can be first forward declared, and defined only later in the program. Are function-local classes an exception from this rule? Please consider a simplified program: auto f() { struct ...
Fedor's user avatar
  • 24.7k
14 votes
1 answer
373 views

Consider the following snippet: int main() { struct Local { virtual void foo() = 0; }; void (Local::* ptr)() = &Local::foo; } When compiling with C++20, GCC 13.3.0 and Clang 18.1.3 ...
OLEGSHA's user avatar
  • 728
1 vote
1 answer
119 views

"You cannot declare an interface inside a block; interfaces are inherently static" This is sentence from https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html#local-classes-...
kaka's user avatar
  • 845
0 votes
0 answers
177 views

A local class in C++ can have friend functions, but these functions cannot be defined neither inside the class [class.friend] p5: A function may be defined in a friend declaration of a class if and ...
Fedor's user avatar
  • 24.7k
0 votes
0 answers
96 views

Hi, I am following the Java Tutorial for Lamda Expressions Approach 3 Specify Search Criteria Code in a Local Class at https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#...
bfin's user avatar
  • 1
3 votes
2 answers
3k views

I have a class with 2 methods. In method1(), I create a local record called Abc. This local record is only available to method1() because it was defined in method1() (here are the rules on it ...
davidalayachew's user avatar
1 vote
2 answers
234 views

This question is mostly about proper Java terminology. I am making the distinction between an inner class, which is tied to instance of its enclosing scope, and an non-inner, nested static class which ...
Gonen I's user avatar
  • 6,187
0 votes
0 answers
227 views

I need Constructor<TestClass> instance for local class TestClass in my JUnit4 test method. public void testMethod() throws NoSuchMethodException { class TestClass { } ...
M. Dudek's user avatar
  • 1,085
2 votes
0 answers
55 views

I have a function named return_local where it returns an instance of a local class enclosed with a lambda. auto return_local() { return [] { static int sample { 5 }; struct Point { int ...
Desmond Gold's user avatar
  • 2,278
0 votes
1 answer
66 views

We known that local class of a class exists (for example: local class inside a method of a class), but can we declare a local class of an interface. I reformat the original of local class as follows: &...
locobe's user avatar
  • 99
1 vote
1 answer
272 views

Source code to scan: template <typename T> class HB { T m; public: void HBfunc1(); }; template <typename T> void HB<T>::HBfunc1() { class HC { public: ...
jw_'s user avatar
  • 1,825
1 vote
4 answers
337 views

Please read the code to know the problem : #include <iostream> void fun(int value) { //starts local class definition class test { int x; public: test(int a) : x(...
user avatar
63 votes
4 answers
3k views

I have the following Java code that uses a local class. import java.util.Arrays; public class X<T> { void m() { class Z {} for (Object o : Arrays.asList(1, 2, 3)) ...
Lukas Eder's user avatar
  • 223k
1 vote
2 answers
1k views

I have a local class... public class Outer { public void wrapper() { class Local { } } } and I have a test that needs to reference the local class... Outer.wrapper.Local....
Brent Bradburn's user avatar
0 votes
1 answer
119 views

I find a very useful to read this question: Java inner class and static nested class, but can't find there any example from Java. Could you provide me with real examples of using those classes from ...
koniga-ganica's user avatar
0 votes
2 answers
132 views

On the subject of Anonymous classes, the Oracle documentation states that... They are like local classes except that they do not have a name. Use them if you need to use a local class only once ...
Zippy's user avatar
  • 3,905
1 vote
1 answer
2k views

I have a program with 4 includes in it. One top-include (global data), one for pai-modules, one for pbo-modules and one for a local helper class. I put the definition and implementation of my local ...
Timur's user avatar
  • 179
0 votes
2 answers
154 views

As I tested, the below code executes without any issues. But I could not understand the logic. Can someone please explain? public static void main(String[] args) { List<String> london = new ...
Shamran Siddique's user avatar
0 votes
0 answers
49 views

So i was working with local classes this morning when I encountered a strange behaviour of inheritance regarding scopes. public class Foo{ public void printer(){ class Hello extends Bar{ ...
L.Spillner's user avatar
  • 1,772
4 votes
3 answers
2k views

I have a following snippet code of c++. Declared a class inside main() function. What is the reason to we can not define friend function in local class? #include<iostream> int main() { ...
msc's user avatar
  • 35.1k
0 votes
1 answer
540 views

Trying to overload input operator >> inside a local class. I tried to define friend istream &operator >> inside class Data. int readFile(char* file_name,float temperature_data[][31]) {...
Gautam Chowdhury's user avatar
2 votes
4 answers
153 views

i was just experimenting with inner classes and came across this idea of having local yet static inner class... well i made an inner class inside a static method.. well it's just simple as that.. Here'...
Dilini Peiris's user avatar
2 votes
4 answers
897 views

Please consider following two classes: a.) Student package datatypes; public class Student { private String name ; public Student(String name) { this.name = name; } class ...
Yati Sawhney - Yates's user avatar
0 votes
0 answers
247 views

Most of the time i see that programmers say Local Classes are not good for some reasons and say instead of creating classes in methods, create new java files and use them. My question is, suppose that ...
HMD's user avatar
  • 470
34 votes
2 answers
2k views

As far as I understand - generic lambdas are transformed into objects of local scope structs with templated operator(). This makes generic lambda very powerful and easy to use tool. On the other hand ...
W.F.'s user avatar
  • 14k
0 votes
1 answer
579 views

I want to write a function that, given an arbitrary java bean as an argument, returns an object that is a copy of that bean but that belongs to an anonymous subclass of the bean's type that contains ...
agtl's user avatar
  • 153
-1 votes
1 answer
75 views

I have a local class declared in a method, whose fields are declared as being private. However, I am still able to access them directly from the body of the enclosing method - why is this? As a side ...
LordCat's user avatar
  • 528
-1 votes
1 answer
39 views

Since we can not use the static modifier with a local class defined inside a method, and since Nonstatic nested classes are Inner classes, we could probably say that a method local class is a type of ...
Solace's user avatar
  • 9,060
-3 votes
2 answers
3k views

In the HelloWorldAnonymousClasses example program (from here): /* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, ...
danger mouse's user avatar
  • 1,497
2 votes
1 answer
100 views

I'm new to Java and is trying to learn the concept of local class. I'm currently reading the chapter on local class on the Offical Java Documentation Oracle. I have encountered two statements in this ...
Thor's user avatar
  • 10.1k
2 votes
3 answers
386 views

(I keep re-reading that question title and thinking about how ridiculous it must look, but I assure you that is the best description of the problem, and I have an actual application where this is the ...
snickers10m's user avatar
  • 1,749
0 votes
2 answers
77 views

I have the following enum: enum FilterFactory { INSTANCE; private final Map<FilterType, Creator> creators; private FilterFactory() { creators = new HashMap<>(); ...
St.Antario's user avatar
  • 27.7k
2 votes
0 answers
25 views

I'm experimenting with local classes in C++ and stuck with following code: void f1(int a) { struct Inner1 { int a; }; struct Inner2 : Inner1 { void foo() {...
hate-engine's user avatar
  • 2,370
6 votes
1 answer
119 views

Consider the following code, that simulates a constexpr lambda (proposed for C++17, not available in C++14). #include <iostream> template<int M, class Pred> constexpr auto fun(Pred pred)...
TemplateRex's user avatar
  • 70.9k
8 votes
4 answers
1k views

class test { public static void main(String[] args) { new test(); } void method() { class inside { int a; void methodinside() {} } ...
newbie's user avatar
  • 979
2 votes
1 answer
130 views

Here is a small sample: public class LocalClassSample { public static void main(String[] args) { class Utils { public void printHello(String name) { System.out....
deostroll's user avatar
  • 12k
1 vote
1 answer
71 views

public class Main { public static void main(String[] args) { int b=1; final int c=2; String s1[] = new String[]{"A","B","C"}; class InnerMain{ ...
Furkan's user avatar
  • 117
2 votes
2 answers
466 views

The JLS 15.9.2 tells us how to determine an enclosing instance: Let C be the class being instantiated, and let i be the instance being created. If C is an inner class, then i may have an ...
St.Antario's user avatar
  • 27.7k
6 votes
3 answers
229 views

Current C++ compilers (latest gcc, clang) require the typename keyword in the example below: template<class T> struct A { }; template<class T> void f(T) { struct C { }; ...
willj's user avatar
  • 3,109
1 vote
1 answer
306 views

Local type as template argument is forbidden in C++03: template<typename T> struct Foo { }; void Make() { struct Unknown {}; Foo<Unknown> foo; // Bad } Is there any directives in ...
DmitriyH's user avatar
  • 450
16 votes
1 answer
1k views

Before Java 8, We were not able to use non-final variables inside local class. But now they are allowing final as well as effectively final(who's values has not been changed), can be referred by local ...
Ashok's user avatar
  • 617
2 votes
2 answers
483 views

I am trying to sort a member variable type std::list using a local function. Since C++ doesn't allow local functions and hence Herb Sutter's suggests local classes, I ended up with the following code. ...
Chenna V's user avatar
  • 10.6k
2 votes
5 answers
5k views

If I have this code. public class Test{ { class People { } } public static void main(String[] args) { People person = new People();//...
Xelian's user avatar
  • 17.3k
1 vote
2 answers
183 views

Please take a look at this snippet: public class A { void method() { System.out.print(B.j);//This is legal! class C { void method () { ...
Mohsen Kamrani's user avatar
3 votes
5 answers
3k views

Java allows me to define local abstract classes, like in this example: public class Foo { public void foo() { abstract class Bar { // Bar is a local class in foo() ... ...
Markus A.'s user avatar
  • 12.9k
3 votes
1 answer
175 views

There are situations that I have to choose local classes over lambda when overloading operator() is not enough or when I need virtual functions or something else. um.. for example: I need a object ...
BlueWanderer's user avatar
  • 2,701
0 votes
2 answers
475 views

I am implementing a certain algorithm in a single method in Java. This algorithm needs a data structure that will not be used anywhere else, so it seems appropriate to me to use a local class. The ...
Brian Kell's user avatar
0 votes
3 answers
755 views

Since a friend function can be declared in a local class as shown in the following example. How can it be used to access members of local class when it is defined in the function definition which ...
user1086635's user avatar
  • 1,544
1 vote
1 answer
102 views

I am trying to setup Dozer to perform a complex mapping between my two entities. Essentially, I want it to convert my percentCompleted double to a boolean, based on if the value is 1 (100%) or not. ...
KallDrexx's user avatar
  • 28k