I am using Soot in order to be able to use its call graph but unfortunately I am having trouble with constructors.
I think this is best explained with an answer so here goes:
Consider a class CachingCollector$NoScoreCachingLeafCollector where NoScoreCachingLeafCollector extends FilterLeafCollector.
I want to get the constructor of such class whereby its parameter types are: LeafCollector and int.
For some reason Soot says that there is a constructor with those parameters BUT the first parameter is CachingCollector.
I cannot understand what is happening and I've been trying for a few hours now to no avail. What is confusing me even more is that there are some inner classes which extend some class but the Soot does not add that extra parameter in the beginning.
Any help would be greatly appreciated!!
Such questions are most quickly answered on the Soot mailing list.
To answer your question: Soot simply shows you what the bytecode actually looks like. The class you are talking about is an inner class. Constructors to such classes are automatically passed a "this" reference to the outer class, such that this outer object can be accessed from within the inner class. That is what Soot shows you.
Related
I am beginner in programming and i just started working with greenfoot (software for creating games using Java). When i was writing code i had to use construction builded in greenfoot which was using parameter described as: java.lang.class where i had to type ClassName.class . I was trying to go through documentation and a lot of other sources to figure out how it works and what is this, but to be honest i couldnt understand anything. Is there is someone who can tell me in simply words what does it mean and how construction like ClassName.class works? That is the first time i see dot notation used like this, before I have seen only dot notation when i tried to refer to for example other class method like: OtherClass.method() . So is it just builded in instance variable of every class or something like this? Thanks in advance
It's called a class literal. ClassName.class provides access to the instance of Class that represents the given class. So for instance, String.class refers to the Class instance for the String class. Using that object, you can do do various things (like find out what its members are at runtime via reflection). (FWIW, objects provide also access to the Class instance for their class via their getClass method.)
A ClassName.class returns the Class object for that particular class. That said and understood, I can't really grasp what the keyword does when used directly in a method.., then, if we write class and follow it immediately with a dot, the list that appears seems to include all that's in scope there, i.e. local variables of the method, other methods and variables(depending on whether the method is static or not), method itself, and class itself..
Coming from its first stated function above, I find myself at odds with this one: I can't tell what it's exactly doing.. way I see it, it's the same word, expect same function at heart, but that doesn't seem to be the case here
Blurry.. I know, but any insight into it is appreciated. Thx.
I get similar behaviour from Netbeans:
However, these are somewhat bogus suggestions.
class.emptyList(), despite being a suggestion, will not compile. This is probably a peculiarity of the way suggestions are computed. If there's a way to get legal code out of these suggestions, I can't think of it.
Note that you get the same suggestions if you just hit CTRL+enter (or whatever keys you have bound to suggestions) on an empty statement.
Usually there are only two circumstances to use the word class:
When defining a class, for example public class MyClass { ... }
With a dot, after a class name, to get the Class object for a type - Class<Person> clazz = Nurse.class
Sometimes you feel you'd like to have a variable called class, but it's a reserved word, so you can't. It's quite common to see variables called clazz for that reason.
I'm new to SO, so if I've done this wrong, please point me in the right direction.
I think this is a bit of an awkward question, because I'm not so great at articulating my thoughts.
At university, we are taught that a java class which is written to be an object, with constructors, getters, setters, etc, are called "Container Classes". They contain data about themselves(a name attribute, for instance).
My question is what are other types of classes? For instance, you have to have a class where you create and manipulate your objects. For a small program this isn't a problem(just put it into the main class/method). For a larger program this would be silly and unmanageable, so obviously you create other classes. I've taken to naming mine "Handler"s. I have a "FileHandler" an "ObjectHandler", etc.
What type of class are these? Are there other class types out there?
Edit: To clarify, I'm wondering about what to call a category of classes, such as classes that are designed to do specific things. Helper classes? Utility classes?
Final Edit: I answered my own question here: https://stackoverflow.com/a/43964279/7985805
At university, we are taught that a java class which is itself an
object
I would have to disagree here, classes are templates for objects, you can think of them as containing a description of a particular object i.e. the object states, the behaviours that object can perform etc. An object is an instance of a class thus a class is not an object.
My question is what are other types of classes?
A class can be any type, it just depends on what you're attempting to accomplish e.g. when making a space invaders game, you could have a class of type Alien(enemies), a class of type Defender(the shooter), a class of type Bullet which the Defender can use to shoot the Aliens etc.
In my opinion if you call 'classes' 'container classes' you are only adding a redundant word.
In larger programs there could be some kind of classes that have the same purpose, e.g there could be ModelData classes, which only hold data: like employee, contract, ComplicatedCalculationResult; or there could be handler classes: FileHandler, MouseHandler, ... But all this is not set to a fixed wording.
You can name your class as it seems fit. At best name it so that someone else can guess what the class is for.
So it turns out after a bit of research, the "class types" I was trying to find names for are actually called "Design Patterns". Things like Builders, Factories, Adapters, etc.
I found a handy tutorial explaining them in further depth here: https://www.tutorialspoint.com/design_pattern/design_pattern_overview.htm
Thanks for trying to answer my very badly worded question. As I said, I'm terrible at trying to explain what I want to say.
In this Java program I'm writing I’m finding that I’m clogging up the main() method with a lot of code that would make it hard to quickly understand/read. Specifically due to a lot of error checking that I’m doing. As a result I want to write a quick function that is only going to be used by this one method to improve readability.
Is writing another method within the class my only option or are there other alternatives?
Is writing another method within the class my only option or are there other alternatives?
It depends:
If the methods should belong only to this class, they should be declared as private or protected methods in the class, depending if the class can be inherited or not.
If the methods should be reused in other classes, it would be better to move it to utility classes as public. For example, check if a String is empty and also validating if is not null. Note that for this example there are methods covering these validations in utility classes from Apache Common Langs, explicitely StringUtils#isEmpty(String).
By the way, Java has methods, no functions.
Answer is yes, generally private methods in the classes hold the code which is not used by outside world. But havig private methods help to reduce the cyclomatic complexity of your public methods. Smaller methods lead to more readable and understandable methods.
Since Java was designed that way, you can only have methods. So, yes, that's the only way. Usually you would use class methods (static) for instance independent methods. These can also be within another class, for example a utility or helper class.
During my interview, interviewer asked me
Can we create class without name ?
Since, I was not sure, if it is really possible to create a class without name. So, I said No.
Later, I tried Googling and found, others are also looking for the answer of the same question, but I didn't found clear answer.
I will appreciate, if anyone clearly explain about this class. I mean, what that class technically known as and how can we instantiate this class ?
Yes, it's called an anonymous class/struct.
In C++:
class {
} x;
x is an object of the type, and you can't create any more, because, well, how would you, given that the class doesn't have a name and all....
how would one call constructor and destructors
You don't. In both Java and C++ constructors and destructors hold the same name as the class (they're not PHP - __construct or whatever), and the missing name kind of gets in the way.
Its also called an anonymous class in Java.
// create a new instance of an anonymous class.
Serializable s = new Serializable() {
};
Note: In the JVM, all classes have a name, it's generated by the compiler for you.
You can't define constructors, but it can have an instance initializer block which does much the same thing.
In java, you can create "anonymous inner classes", for a detailed answer see How are Anonymous (inner) classes used in Java?