"error: main method not found", even if it's there [closed] - java

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 8 years ago.
Improve this question
I'm a beginner with Java and I'm trying some exercises with variable types. I have just copied the following exercise and I'm trying to run it with NetBeans 7.4 (on a Windows computer):
public class ClassVariables {
public static class Employee{
private static double salary;
// 'DEPARTMENT' is a constant:
public static final String DEPARTMENT = "Development ";
public static void main(String[] args){
salary = 1000;
System.out.println(DEPARTMENT + "average salary: " + salary);
}
}
}
There is no error or warning shown in the editor but when I run the code I get an error message that says:
"Error: Main method not found in class classvariables.ClassVariables, please define the main method as:
public static void main(String[] args)"
I have already defined my main method as required but I keep getting this error message!
Could somebody help me out, please?

The outer class ClassVariables does not contain main method. The main method is written inside inner class. And you are trying to run the main method from class ClassVariables which is not available.

Move your main method outside the scope of Employee class and place it inside the class ClassVariables

Related

Wrapper classes used in class generics [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
In java, after using a particular wrapper class in the generics of that class, we can't use that particular wrapper class in any static method or instance method or instance variable of that class. And another problem is the constructor which can only accept Integer object is accepting Strings(or any other wrapper class object) too. Look at the code below,what is the reason behind these compilation errors?
public class Exp<Integer> {
Integer val1;
Integer val2=new Integer(2);//compilation error, cannot instantiate the type Integer
public Exp(Integer arg1){
System.out.println(arg1);
val1=arg1;
}
public void lol(){
// Integer intValue2=new Integer(2);//compilation error, cannot make static reference to the non static type Integer
}
public static void main(String[] args){
Exp obj1=new Exp(10);
// Exp obj2=new Exp("Hello world");
// Exp<String> obj3=new Exp<String>("sss");// The constructor takes Integer arg, then why is string arg working perfectly with it?
String str="10";
Character c=new Character('c');//works perfectly
Float f1=3.0f;
Integer intValue1=new Integer(2); //**compilation error, cannot make static reference to the non static type Integer**
Exp<Integer> obj4=new Exp<>(10); //**compilation error, cannot make static reference to the non static type Integer**
}
}
Here you are not using "wrapper class in the generics", you just named your generic type variable as an existing class in java.lang package which hides the original class. However you may still access the original class using fully-qualified name:
java.lang.Integer val2 = new java.lang.Integer(2);
The same for other places where you have compilation error. In general it's better to avoid such names which clash with java.lang classes. Probably you actually wanted to write something different, like
public class Exp extends SomeOtherGenericClass<Integer> { ... }
The type in angle brackets is a dummy which is substituted for the actual type later. It is common to use <T>. You have used a real type, <Integer> which hides the system class Integer, so Integer in your program is no longer referring to java.lang.Integer, leading to the error messages.
Your code should look like this:
public class Exp<T> {
T val1;
...

Variable Setting to All Subclasses [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
Is there a way to insantiate a variable that encompasses all subclasses of a class? From what I've read so far we must state what type the variable is before setting it equal to something:
Example:
ExampleObject1 object = reference to the object
But what if we wanted to make it so that we could set the variable to any instance or subclass of that object?
Yes, you can already do that.
A variable of type T (as long as T is a class/interface/enum/annotation) can hold a reference to any instance of the class T, or any instance of a class that extends or implements T.
For example, this works:
class MyClass1 {
// ... stuff goes here ...
}
class MyClass2 extends MyClass1 {
// ... stuff goes here ...
}
class Main {
public static void main(String[] args) {
MyClass1 object = new MyClass2();
}
}

Program flow from main [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 8 years ago.
Improve this question
Suppose
public class A{
public void doSomethingInMethodA(){
int a =0;
System.out.println("value of a :"+a);
}
public static void main(String[] args) {
new A().doSomethingInMethodA();
new B().doSomethingInMethodB();
}
}
class B{
public void doSomethingInMethodB(){
int b =0;
System.out.println("value of b :"+b);
C c = new C();
c.doSomethingInMethodC();
}
}
class C{
public void doSomethingInMethodC(){
int c =0;
System.out.println("value of c :"+c);
}
}
Now how can I know the flow of the program programatically.
Like If I provide the name of class A(class with main method) to some parser class then the parser class should tell me the flow of the program like
Edit:
A(class with main)
-> doSomethingInMethodA()(Class A)
-> doSomethingInMethodB()(Class B) ->doSomethingInMethodC()(Class C)
program flow ends
where -> means it calls
I guess through reflection it is not possible because you can't ask a java.lang.reflect.Method which methods do you call?
Is it possible with StackTraceElement? I even heard of AspectJ doing something like this
So how to achieve it?
If you want to generically record the execution flow for any given program, you can use bytecode instrumentation. Consider using java.lang.instrument and ASM to achieve your goal. Take a look at this tutorial.
It's not easy by any means, but it's certainly doable.

instantiation of objects of other classes in 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 8 years ago.
Improve this question
i have this situation:
public class Number {
int num;
private TakeNumber take = null;
public Number() {
num = 5;
}
public void print() {
take.doSomething();
}
public int getNumber() {
return num;
}
public static void main(String[] args) {
new Number();
}
}
public class TakeNumber {
private Number number = new Number();
public void doSomething() {
System.out.println(number.getNumber());
}
}
Now, can someone explain me these situations:
I want to know what the compiler interprets here : private Number number = new Number();
Initialize the object in question and passing the required methods
Is correct initialize one object to null and then call a function on that object, as shown
in brief
I would like to know if you can call a method of a class of another class:
without the required function to be static
IMPORTANT do not inherit the classes because I want to use methods of these classes, for example:
I have classes that are conceptually different as Tomato and machine, I would call the methods of Machine into Tomato
I would like to know if you can call a method of a class of another class:
without the required function to be static
If the methods are not static, then you will need an instance of that object to call it.
Object o = new Object();
o.doSomething();
Is how you access an instance method.
I have classes that are conceptually different as Tomato and machine, I would call the methods of Machine into Tomato
This is fine. This is called composition. Composition is a has a relationship between classes. A class Man has a class Car, but Is a class Person. And it is perfectly valid to write code as you have shown. This is how you use composition to expose only the interface of a composite object that you want. For example..
public class MyClass extends MyOtherClass
Now you've just exposed the whole interface of MyOtherClass. This might not be desired.
public class MyClass {
MyOtherClass otherClass;
public void doSomething() {
otherClass.doSomething();
}
}
Now, you've only exposed the doSomething() method. This is useful when, as you said, your objects are conceptually different, but require some shared functionality. It is a perfectly valid code practise.
NOTE: Given the confusing nature of your question, I imagine I've missed some stuff out so please comment with desired edits.

why java does not support dynamic variable dispatch [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Please excuse me if title is wrong. There are two class Test and TestChild1 where TestChild1 is inherited from Test. Both classes have a variable named "a". When I tried to access variable "a" through superclass variable which is instantiated with subclass object, it is giving the value that is initialized with in superclass not subclass.
following is the code that raised the doubt
class Test {
public int a = 10;
}
class TestChild1 extends Test {
public int a = 20;
}
class Main {
public static void main(String args[]) {
Test test = new TestChild1();
System.out.println(test.a); // results in 10
}
}
Please give me the reasons for this behavior. Thanks in advance....
Because the Java designers decided to make methods polymorphic (and thus overridable), but not fields.
When you reference a field from an object, the compiler decides which field to use, based on the declared type of the variable, which, in this case, is Test.
When you refer to methods, the JVM, at runtime, chooses which method to call based on the actual, concrete type of the object which, in this case, is TestChild.
OO is all about encapsulation of state, so you should almost never expose fields to the outside anyway.
The class TestChild1 has two variables with the same name. If you access them through Test you get the first one, from TestChild1 you get the second one.
To get your expected result, you should not declare a in the derived class. Instead you should initialize it in the costructor of the derived class.
You declared your object as Test, not the subclass. At compile time that means you refer to the base class which has 10.
Because behavior is associated with methods and not with fields.
So, fields have static binding (in this case this, since test is of type Test, value of a is assigned with value 10). Whereas methods have dynamic binding.
Since, the variable a doesn't define the behavior of Test class, it is assigned the value as per its type and not as per its instance.
JB Nizet have already said everything, but I will add this code for more understanding:
class Test {
private int a = 10;
public int getA() {
return a;
}
}
class TestChild1 extends Test {
private int a = 20;
public int getA() {
return a;
}
}
class Main {
public static void main(String args[]) {
Test test = new TestChild1();
System.out.println(test.getA()); // results in 20
}
}
So if you would encapsulate your fields, you would have expected behaviour.

Categories

Resources