Program flow from main [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
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.

Related

How to call another classes super class? [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 3 years ago.
Improve this question
Lets say I have three classes, A,B,C. B inherits A, is there a way to access the getVal method of Class A, in C?
class A {
getVal method
}
class B extends A {
}
Class C {
main() {
B x = new B
x.getVal?
}
Yes. Assuming the classes remain in the same package - x.getVal will work.
class A {
String getVal(){
return "from a";
}
}
class B extends A {
}
public class C {
public static void main(String [] args) {
B x = new B();
x.getVal();
}
}
It works - because of the default access modifier. Use the protected access modifier for inheritance.

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();
}
}

"error: main method not found", even if it's there [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
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

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.

Is it possible to access child class variable through parent class reference? [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 9 years ago.
Improve this question
Can someone pls let me know the output of this code?
public Class A {
public int count = 5;
public void test() {
// some code
}
}
public Class B extends A {
public int count = 10;
public void test() {
//some code
}
}
public Class Check {
A a = new A();
A b = new B();
public void myTestMethod() {
a.count; //should call A?
a.test; //should call A?
b.count; //which count is called here? compiler error?
b.test; //should call B?
}
}
a.count; => yes you can
a.test; => yes you can
b.count; => count = 5; its didn't shows the compiler error its return the 5.
b.test; => Yes
Java inheritance lets the extending test over-ride or hide methods from the class it extends. So if B extends A and both have the same method then when you call b.method() it will call that method in B.
Inside B you can choose which method is called by doing either method() or super.method() to specify whether to call the super implementation or not.

Categories

Resources