Getting class by its name
use forName instead..
something like this..
try {
Class<?> act = Class.forName("com.bla.TestActivity");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Java - get the current class name?
The "$1" is not "useless non-sense". If your class is anonymous, a number is appended.
If you don't want the class itself, but its declaring class, then you can use getEnclosingClass()
. For example:
Class<?> enclosingClass = getClass().getEnclosingClass();
if (enclosingClass != null) {
System.out.println(enclosingClass.getName());
} else {
System.out.println(getClass().getName());
}
You can move that in some static utility method.
But note that this is not the current class name. The anonymous class is different class than its enclosing class. The case is similar for inner classes.
How to get a Class Object from the Class Name in Java
You can use:
Class c = Class.forName("com.package.MyClass");
And later instantiate an object:
Object obj = c.newInstance();
EDIT: This is just the simplest use case. As indicated in the comments, you will need to consider constructor arguments and exceptions thrown by the initialization process. The JavaDocs for newInstance
has all the details.
Get class name using jQuery
After getting the element as jQuery object via other means than its class, then
var className = $('#sidebar div:eq(14)').attr('class');
should do the trick. For the ID use .attr('id')
.
If you are inside an event handler or other jQuery method, where the element is the pure DOM node without wrapper, you can use:
this.className // for classes, and
this.id // for IDs
Both are standard DOM methods and well supported in all browsers.
getting only name of the class Class.getName()
Class.getSimpleName()
How to get element by class name?
The name of the DOM function is actually getElementsByClassName
, not getElementByClassName
, simply because more than one element on the page can have the same class, hence: Elements
.
The return value of this will be a NodeList instance, or a superset of the NodeList
(FF, for instance returns an instance of HTMLCollection
). At any rate: the return value is an array-like object:
var y = document.getElementsByClassName('foo');
var aNode = y[0];
If, for some reason you need the return object as an array, you can do that easily, because of its magic length property:
var arrFromList = Array.prototype.slice.call(y);
//or as per AntonB's comment:
var arrFromList = [].slice.call(y);
As yckart suggested querySelector('.foo')
and querySelectorAll('.foo')
would be preferable, though, as they are, indeed, better supported (93.99% vs 87.24%), according to caniuse.com:
- querySelector(all)
- getElementsByClassName
- Don't use w3schools to learn something
- Refer to MDN for accurate information
Get class name and package of VariableElement
You can use the VariableElement
s asType()
method to get a TypeMirror
object and then the toString()
method should return a "source code" representation of the type. There's also some utility function in Types
that can be used with TypeMirror
s.
How to get class name of any java file
Well .java files need to have the same name as the class or enum within, so we could just use the file name:
public String getClass(File file){
return removeExtension(file.getName());
}
removeExtension
has many different ways of achieving, here is just one:
public static String removeExtension(String file){
return file.replaceFirst("[.][^.]+$", "");
}
More here: How to get the filename without the extension in Java?
...the reason behind me wanting to do is is so that I can count the methods within the class.
OK, well this is not the way to do it, you should look into reflection: What is reflection and why is it useful?
Get the name of a Dart class as a Type or String
The class type can be used as a Type
:
Type myType = MyClass;
Related Topics
Kvo Listener Issues in Swift 4
Swiftui Multiple Animations for Same Element
Create Custom Action in a Class for Use in Interface Builder
Parsing JSON from Url Ends Up with an Error - Swift 5
Can Not Get Correct Position of View in Swiftui
Wrong Image Orientation After Displaytransform Call
Idiomatic Way to Test Swift Optionals
Swift Unsafemutablepointer & Unsafemutablepointer<Unsafepointer<Sometype>>
Modify Scpreferences Persistent Storage: Invalid Argument
Uibutton Background Color Overlaps Text on Highlight
How to Programmatically Change The Xdr Display Reference Mode (Aka Preset) on Macos
Lazy Initialization and Deinit
How to Delay a Return-Statement in Swift
Error Domain=Nscocoaerrordomain Code=3840 "Invalid Value Around Character 0
Root Class of All Classes in Swift
Aurendercallbackstruct in Swift
Error: Variable with Getter/Setter Cannot Have an Initial Value