Is it possible to declare a constructor inside a method [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking for a ans what I have found in facebook and became confused
"is it possible to declare a constructor inside a method"

Short answer: no.
Long answer: This comes from the Java Language Specification, §8.8:
A constructor is used in the creation of an object that is an instance of a class.
In all other respects, the constructor declaration looks just like a method declaration that has no result (§8.4.5).
Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.
It's declared like this:
ConstructorDeclaration:
ConstructorModifiers(opt) ConstructorDeclarator
Throws(opt) ConstructorBody
ConstructorDeclarator:
TypeParameters(opt) SimpleTypeName ( FormalParameterList(opt) )
A ConstructorDeclarator can only live inside of the class body declaration:
ClassBody:
{ ClassBodyDeclarations(opt) }
ClassBodyDeclarations:
ClassBodyDeclaration
ClassBodyDeclarations ClassBodyDeclaration
ClassBodyDeclaration:
ClassMemberDeclaration
InstanceInitializer
StaticInitializer
ConstructorDeclaration <--
ClassMemberDeclaration:
FieldDeclaration
MethodDeclaration
ClassDeclaration
InterfaceDeclaration
;
A MethodDeclaration has no symbol to a ConstructorDeclaration, which is why you cannot declare a constructor inside of a method.

You cannot declare a constructor inside a method.
Constructors and methods are both components of objects. Your object has a constructor that gets called when you instantiate it. Once instantiated, your object then has properties to define it, and methods to do things with.
If you try to place a constructor into a declaration, you will get a compilation error.
This comes from several years of practice and study for the Sun certification tests.

No it is not possible.java does not support inner methods concept.Constructor is also like a method.Constructors can be declared inside a class only.

The constructor is sort of a method much like the main method. The constructor is used to initialize an object of the class and it has no return value.

Not at all possible.
A constructor must invoked while creating object for a class.
With out instance how you will call that method,which is building the constructor?.
Making any sense ?

Related

In Java, if a method uses a static member, why should itself be declared as static? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I understand that in a static method, non-static members should be called with related objects, or there would be ambiguity.
If my understand is correct?
In Java, if a method uses a static member, why should itself be declared as static?
This is not true - a method that uses a static member does not need to be static itself.
I understand that in a static method, non-static members should be called with related objects, or there would be ambiguity.
If my understand is correct?
No.
When a member variable or method is static, it means that this member variable or method isn't part of, or doesn't work on one specific object of the class; it's shared by all objects of the class. The section Understanding Class Members in Oracle's Java Tutorials explains this in more detail.
Non-static methods work on a specific object, so if you call them from a static method, you have to call them on an object, since there is no current object (which this refers to) when you're in a static method.

"Modifier 'private' not allowed here" - why not? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to know why this problem occurs.
public class Sorter {
public static char[] selectSort(String targetStr) {
private char[] charArray = new char[targetStr.length()];
The problem occurs when the variable charArray is made private and there is a message:
Modifier 'private' not allowed here.
Can anyone explain to me thoroughly why this happens so that I can better handle them in the future?
You can't have access modifiers to method local variables.
All the method local variables are visible only inside the method.
You cannot use access modifiers with fields declared within method scope (i.e. local fields).
So neither private nor public nor protected.
Here is a tutorial on Java variables that broaches the subject.
That char array(charArray ) is already local to that method., You cannot access that outside of that method anyway. So access modifier doesn't make sense there.
Scope of that variable is only till that method it cant be used outside that so there is no need to give access specifier to it.
Any variable created inside a method is local to that method only and it cant be accesed outside.
Scope of that variable char Array is only till that method it cant be used outside that so there is no need to give access specifier to it.

Java: Calling non-static method with this. from static method [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was wondering if I could use this. to call a non-static method from within a static method. I know I would need an object in general to reference to a non-static method from within a static method. Thanks
No. Within a static method, this has no meaning and will not compile. This is covered by §8.4.3.2 of the Java Language Specification, although it should be fairly intuitive — what would thisrefer to?
You can call non-static methods from static methods, just not via this. You have to have an instance on which to call them.
You cannot use "this" keyword in a static method.
The answer is NO. A static method is not associated with an instance of the class, so it cannot access a non static variable or method of the same class that has a meaning only if there is an instance of the class
Not using this, but if you really wanted to, if the class was named MyClass, you could do
new MyClass().someNonStaticMethod()
But if you're calling instance methods like this, they should probably be static anyway.

what is these methods "with" and "and" in java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am reviewing a new project code. I found the following line:
crudService.findByNamedQueryFirstResult("TableA.findby.custid", with(
"custID", IndividulaID).and("flags", custflags).parameters())
I don't see methods "with" and "and" in any class. And its not string concatenation.
I know a JPQ query has with and and. But how is it possible to pass params like above?
Can someone help?
There are no with or and keywords in Java. Those are method calls.
Here, the with() method is imported via a static import (see #damo's comment) and creates an instance of a builder class; the and() method is a method which returns this; the .parameters() class will then build the class, and return what the calling method actually needs. This is a classical builder pattern.
Static imports can be used to great readability effects. Consider mockito. Either you write:
Mockito.when(xxx).then(xxx);
or:
import static org.mockito.Mockito.*;
// the compiler know where `when()` comes from
when(xxx).then(xxx);
The generic pattern for such constructs is called a fluent interface. To complete the answer, I'd just point to what I have done for one of my projects:
ProcessorSelector<IN, OUT> selector = new ProcessorSelector<IN, OUT>();
selector = selector.when(predicate1).then(p1)
.when(predicate2).then(p2)
.otherwise(defaultProcessor);
final Processor<IN, OUT> processor = selector.getProcessor();
The ProcessorSelector class has a when() method; this method returns another class on which there is a then() method; and the then() method returns, again, a ProcessorSelector, which has when(), etc. Finally, the otherwise() method returns a ProcessorSelector on which you have a .getProcessor() returning a Processor.
This looks like a fluent-api (see http://java.dzone.com/articles/java-fluent-api-designer-crash for general definition)
In your case it may be spring-jpa, QueryDSL or similar framework.
To identify the API, you should have a look at the type/superclass/interfaces of crudService as well as the definitions of with/and/parameters.

Reflection, inheritance, and java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm wondering how does reflection on inherited classes work? It appears that the methods are not reflectively available in my superclasses constructor.
If I have a subclass "horse" of "animal", is it possible to access the methods of "horse" using reflection when I am in the "animal" superclasse's constructor? It appears not.
SuperClass constructor is always run before the child class's. So the instance of child class is never in ready state to be used as such.
Is it not possible, because constructor body of animal is invoked before body of constructor horse. Horse 'part' of object doesn't exist yet.
A superclass's ctor (or anything else, really) shouldn't be accessing sub-class-specific methods; the superclass should not know anything about classes that derive from it.
For that matter, superclasses shouldn't call methods defined by subclasses in its ctor because the object hasn't been initialized yet. Here's some discussion regarding that, with examples.
If you have an instance of a Horse, it doesn't matter where you are (including int he Animal constructor) you can access all the methods/fields of that instance.
However, if you are in a plain Animal or some other subclass, you can't access methods/fields of Horse because its not a Horse.
Yes and no. Via the reflection API you can access the methods of a subclass (or any class) if you're in a constructor. In terms of good practice, then no since an animal is not a horse.
The important thing to realize is the way that java loads classes : first the superclass is loaded, and then the subclasses.
if you need to access methods that are specific to the subclass, you will have to find another methodology.
One strategy , for example, would to embed some of the contract which your subclass is fulfilling in the superclass using interfaces or inheritance....

Categories

Resources