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

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.

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.

Does Java require keyword "public" in class variables? [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 know that in JavaScript, you don't need the public keyword in the following code :
class myClass
{
public int myVariable;
// it is the same as :
int myVariable
}
do you need it in Java ? What is its purpose ?
Yes if you want something to be accessible everywhere.
Otherwise it is package-visibility, meaning only stuff in the same package (at some level) can access it.
You don't 'need' the 'public' keyword - if you don't specify the access level of a Class variable it will be set to package-private.
More details are here -
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
public fields are a bad idea, however (A VERY bad idea in a multi-threaded application). It allows other classes to change the state of your class without any control, and could break invariants. The proper way is to control state-changing through public setter methods.

How to create a class in java which instance cannot be created? [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 have to create a class. Instance of this class cannot be made. How can I achieve that?
Declare it abstract and add a private constructor.
Do you mean no instances can ever be made? or just one instance?
If no instances can ever be made, then make the class final with a private constructor. All methods then need to be static. A good example of this is java.lang.Math
If you need only a single instance and want no one else to make new instances, then consider using an enum as described in Effective Java 2nd ed:
public enum MyClass{
INSTANCE;
...methods
}
Code that uses this class then invokes methods like this: MyClass.INSTANCE.foo().
If you don't want to instantiate that class then I assume that you only want it to have some static methods. In that case you can easily do it with enum with no constants like
enum MyUtilities{
;//if you want you can place instances of that class here, or not place any
//your methods
public static void myMethod(){
System.out.println("hello");
}
}

Is it possible to declare a constructor inside a method [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 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 ?

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.

Categories

Resources